如何使用system.xml将“sub”子项添加到具有属性的节点

时间:2016-01-20 12:37:32

标签: c# xml xmldocument

祝你新年快乐! 我得到了以下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等“部件”中添加Child?

我想创建一个循环(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) ...

提前致谢

2 个答案:

答案 0 :(得分: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表达式(只是说,因为你写了&#34;我使用VS2015,C#,不使用Linq或XPath等&#34; )。

答案 1 :(得分:0)

使用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));
            }
        }
    }
}