在 xml 中添加额外的节点

Add extra node in xml

提问人:Sribin 提问时间:7/19/2023 最后编辑:Yitzhak KhabinskySribin 更新时间:7/19/2023 访问量:56

问:

我正在尝试修改 xml 并在其中添加一个额外的节点。到目前为止,我能够添加这个节点,但我需要一些调整。

我的示例 XML

<Search Name="Test2" ProjTypeID="107">
<CritGroup CritGroupID="1" >
    <Crit CritID="205"/>
    <Crit CritID="208"/>
</CritGroup></Search>

我可以添加额外的节点,但我需要将其添加为现有的“Crit”节点格式(在没有关闭“Crit”的情况下,使用/>)。但我最终得到了以下输出。

<Search Name="Test2" ProjTypeID="107">
<CritGroup CritGroupID="1" >
    <Crit>CritID="206"</Crit>
    <Crit CritID="205"/>
    <Crit CritID="208"/>
</CritGroup> </Search>

我使用了下面的代码

            XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "Crit", "");
            newNode.InnerText = "CritID=\"206\" ";

            XmlNode CritGroupNode = xmlDoc["Search"]["CritGroup"];
            XmlElement groups = CritGroupNode["CritGroup"];
            CritGroupNode.InsertAfter(newNode, groups);

还有什么方法可以把新节点放在最后!现在它被添加为第一个?感谢您的阅读和帮助!

C# asp.net Linq-to-XML

评论

0赞 Yitzhak Khabinsky 7/19/2023
XML 格式不正确。缺少根节点。
0赞 Sribin 7/19/2023
我已经更新了
0赞 Yitzhak Khabinsky 7/19/2023
最好使用 LINQ to XML,而不是过时的 XML API(大约 2002 - 2006 年)。
0赞 KulaGGin 7/19/2023
<Crit>CritID="205"</Crit>不等同于 。在 XML 中设置属性的方法是使用其他元素。最接近的是.编号: w3schools.com/xml/xml_attributes.asp<Crit CritID="205"/><Crit CritID="205"/><Crit><CritID>"205"</CritID></Crit>

答:

2赞 Yitzhak Khabinsky 7/19/2023 #1

通过LINQ to XML非常容易。

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<Search Name='Test2' ProjTypeID='107'>
            <CritGroup CritGroupID='1'>
                <Crit CritID='205'/>
                <Crit CritID='208'/>
            </CritGroup>
        </Search>");
    xdoc.Dump("Before");

    XElement CritGroup = xdoc.Descendants("CritGroup")
        .Where(x => x.Attribute("CritGroupID").Value == "1").FirstOrDefault();
    
    CritGroup.Add(new XElement("Crit",
        new XAttribute("CritID","206"),
        new XAttribute("RelID","280"),
        new XAttribute("CompValue","11")));
    
    Console.WriteLine(xdoc);
}

输出

<Search Name="Test2" ProjTypeID="107">
  <CritGroup CritGroupID="1">
    <Crit CritID="205" />
    <Crit CritID="208" />
    <Crit CritID="206" RelID="280" CompValue="11" />
  </CritGroup>
</Search>

评论

0赞 Sribin 7/19/2023
感谢您的回答,您能否告诉我如何添加更多项目以及 CritID。像 <Crit CritID=“206” RelID=“280” CompValue=“11” CompValueHuman=“Yes” AndOr=“AND” TextBoxName=“” /> 之类的东西。谢谢!。
1赞 Yitzhak Khabinsky 7/19/2023
我更新了答案。LINQ to XML 使它变得非常容易。您可以根据需要添加任意数量的 XML 属性。
0赞 Sribin 7/19/2023
我的要求现在稍微调整一下,如果存在任何 CritID=205 的元素,我想添加新元素作为第二个元素。
0赞 Yitzhak Khabinsky 7/19/2023
请发布一个全新的问题,并提供一个最小的可重复示例
0赞 Serge 7/19/2023 #2

你可以试试这个代码

    XmlElement newElement = xmlDoc.CreateElement("Crit");
    newElement.SetAttribute("CritId", "204");

    XmlNode CritGroupNode = xmlDoc["Search"]["CritGroup"];
    XmlElement groups = CritGroupNode["CritGroup"];
    CritGroupNode.InsertAfter(newElement, groups);
-1赞 nikstra 7/19/2023 #3

我会在另一个答案中使用该解决方案,但如果这不是一个选项,这将最后插入一个新元素。XDocumentCrit

XmlElement newNode = xmlDoc.CreateElement("Crit");
newNode.SetAttribute("CritID", "206");

XmlNode CritGroupNode = xmlDoc["Search"]["CritGroup"];
CritGroupNode.InsertAfter(newNode, CritGroupNode.LastChild);