Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用system.xml向具有属性的节点添加“子”子节点_C#_Xml_Xmldocument - Fatal编程技术网

C# 如何使用system.xml向具有属性的节点添加“子”子节点

C# 如何使用system.xml向具有属性的节点添加“子”子节点,c#,xml,xmldocument,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">

祝你新年快乐! 我得到了以下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的子节点


提前感谢

使用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));
            }
        }
    }
}
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的这一部分;谢谢你的帮助@har07的解决方案更适合,谢谢,但我不确定您是否想从编译器单元获取id并插入到部件中。新的身份证号码来自哪里?
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);
}