如何使用 system.xml 将 "sub" child 添加到具有属性的节点
How can i add "sub" child to a node with attribute using system.xml
祝您新年快乐!
我得到了以下 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 到 "Parts" 中?
我想创建一个循环 (for-loop),它在 "Parts" 中为每个 "SW.CompileUnit"-Node
创建一个 child
你能帮帮我吗?
PS: 我用的是VS2015、C#,没有用Linq、XPath等
直到现在我添加了一个 child 这样的:
XmlNode xPiece = xdoc.SelectSingleNode("//NS2:Parts",nsmgr);
xPiece.AppendChild(myXMLElement);
但它只在第一个SW.CompileUnit节点(ID=1)中添加了一个child
...
提前致谢
使用 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()
returns 只匹配第一个元素。要获取所有匹配的元素,您应该使用 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表达式(只是说,因为你写了"I use VS2015, C#, not using Linq or XPath etc")。
祝您新年快乐! 我得到了以下 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 到 "Parts" 中?
我想创建一个循环 (for-loop),它在 "Parts" 中为每个 "SW.CompileUnit"-Node
创建一个 child你能帮帮我吗?
PS: 我用的是VS2015、C#,没有用Linq、XPath等
直到现在我添加了一个 child 这样的:
XmlNode xPiece = xdoc.SelectSingleNode("//NS2:Parts",nsmgr);
xPiece.AppendChild(myXMLElement);
但它只在第一个SW.CompileUnit节点(ID=1)中添加了一个child ...
提前致谢
使用 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()
returns 只匹配第一个元素。要获取所有匹配的元素,您应该使用 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表达式(只是说,因为你写了"I use VS2015, C#, not using Linq or XPath etc")。