【问题标题】:How can i add "sub" child to a node with attribute using system.xml如何使用 system.xml 将“子”子节点添加到具有属性的节点
【发布时间】:2016-01-20 12:37:32
【问题描述】:

祝你新年快乐! 我得到以下 XML 结构:

<?xml version="1.0" encoding="UTF-8"?>
<SW.CodeBlock ID="0">
    <SW.CompileUnit ID="1">
        <AttributeList>
            <NetworkSource>
                <FlgNet xmlns="http://www.TEST.com">
                    <Parts> </Parts>
                </FlgNet>
            </NetworkSource>
        </AttributeList>
    </SW.CompileUnit>
    <SW.CompileUnit ID="2">
        <AttributeList>
             <NetworkSource>
                 <FlgNet xmlns="http://www.TEST.COM">
                    <Parts> </Parts>
                 </FlgNet>
             </NetworkSource>
        </AttributeList>
    </SW.CompileUnit>
</SW.CodeBlock>

如何在 SW.CompileUnit ID = 1 和 SW.CompileUnit ID = 2 等的“部件”中添加子项?

我想创建一个循环(for-loop),它为每个“SW.CompileUnit”-Node 在“Parts”中创建一个子节点

你能帮帮我吗?

PS:我用的是 VS2015,C#,没有使用 Linq 或 XPath 等。

直到现在我像这样添加一个孩子:

XmlNode xPiece = xdoc.SelectSingleNode("//NS2:Parts",nsmgr);
xPiece.AppendChild(myXMLElement);

但它只在第一个 SW.CompileUnit 节点中添加了一个子节点(ID=1) ...

提前致谢

【问题讨论】:

    标签: c# xml xmldocument


    【解决方案1】:

    SelectSingleNode() 只返回第一个匹配的元素。要获取所有匹配的元素,您应该使用 SelectNodes() 代替:

    var nodes = xdoc.SelectNodes("//NS2:Parts",nsmgr);
    foreach(XmlNode node in nodes)
    {
        //create new myXMLElement
        ....
        //and then append it to current <Parts>
        node.AppendChild(myXMLElement);
    }
    

    顺便说一句,SelectNodes()SelectSingleNode() 的参数是 XPath 表达式(只是说,因为你写了 “我使用 VS2015、C#,而不使用 Linq 或 XPath 等”)。

    【讨论】:

    • 非常感谢您的帮助,是的,我想我只是错过了 Xpath 的那一部分;)
    【解决方案2】:

    使用 XML Linq。不确定我的要求是否完全正确。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xml =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<SW.CodeBlock ID=\"0\">" +
                        "<SW.CompileUnit ID=\"1\">" +
                            "<AttributeList>" +
                                "<NetworkSource>" +
                                    "<FlgNet xmlns=\"http://www.TEST.com\">" +
                                        "<Parts> </Parts>" +
                                    "</FlgNet>" +
                                "</NetworkSource>" +
                            "</AttributeList>" +
                        "</SW.CompileUnit>" +
                        "<SW.CompileUnit ID=\"2\">" +
                            "<AttributeList>" +
                                 "<NetworkSource>" +
                                     "<FlgNet xmlns=\"http://www.TEST.COM\">" +
                                        "<Parts> </Parts>" +
                                     "</FlgNet>" +
                                 "</NetworkSource>" +
                            "</AttributeList>" +
                        "</SW.CompileUnit>" +
                    "</SW.CodeBlock>";
    
                XDocument doc = XDocument.Parse(xml);
                var compileUnits = doc.Descendants("SW.CompileUnit").Select(x => new {
                    ID = (string)x.Attribute("ID"),
                    parts = x.Descendants().Where(y => y.Name.LocalName == "Parts").FirstOrDefault()
                }).ToList();
                foreach (var compileUnit in compileUnits)
                {
                    compileUnit.parts.Add(new XElement(compileUnit.parts.Name.Namespace + "ID", compileUnit.ID));
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助! @har07 的解决方案更合适,不过谢谢
    • 我不确定您是否想从 CompileUnit 中获取 id 并插入到 Parts 中。新身份证号从哪里来?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多