xml:<?xml version="1.0" encoding="utf-8"?>
<Loops>
<Loop>
<Po>
<BillNo>12346</BillNo>
<Qty>12346</Qty>
</Po>
<LOOP_PID_g006>
<PID>
<e01_0349>F</e01_0349>
<e03_0559>ZZ</e03_0559>
<e04_0751>OD</e04_0751>
<e05_0352>12 GSK 77 FS-FDA-A</e05_0352>
</PID>
</LOOP_PID_g006>
</Loop>
</Loops>引用using System.Xml.Linq;
代码: string path7 = @"D:\20141103.xml";
XElement root = XElement.Load(path7);
var loop = root.Elements("Loop").FirstOrDefault();
root.Add(loop);
root.Save(path7);
<Loops>
<Loop>
<Po>
<BillNo>12346</BillNo>
<Qty>12346</Qty>
</Po>
<LOOP_PID_g006>
<PID>
<e01_0349>F</e01_0349>
<e03_0559>ZZ</e03_0559>
<e04_0751>OD</e04_0751>
<e05_0352>12 GSK 77 FS-FDA-A</e05_0352>
</PID>
</LOOP_PID_g006>
</Loop>
</Loops>引用using System.Xml.Linq;
代码: string path7 = @"D:\20141103.xml";
XElement root = XElement.Load(path7);
var loop = root.Elements("Loop").FirstOrDefault();
root.Add(loop);
root.Save(path7);
你问题描述清楚了么? 自己手动拼接节点。
http://blog.csdn.net/duanzi_peng/article/details/24018431
你问题描述清楚了么? 自己手动拼接节点。
http://blog.csdn.net/duanzi_peng/article/details/24018431
按着你的方式,得有一个模版文件。如果没有模版,直接生成,应该如何处理,谢谢!
new XAttribute("Id", entity.Id),
new XAttribute("Width", entity.Width),
new XAttribute("Height", entity.Height),
new XAttribute("IsSelected", entity.IsSelected.ToString()),
new XAttribute("CollectionName", entity.CollectionName),
new XAttribute("GroupName", entity.GroupName),
new XElement("Name", entity.Name),
new XElement("IP", entity.IP),
new XElement("X", entity.X),
new XElement("Y", entity.Y)
);
this.xmlDoc.Add(carElement);
string xpathLOOP_PO1_g005 = "//xsi:Body/xsi:X12855/xsi:LOOP_PO1_g005";
string xpathPO1 = "//xsi:Body/xsi:X12855/xsi:LOOP_PO1_g005/xsi:PO1";
for (int kk = 0; kk < 4; kk++)
{
AddParentNode(xml, "LOOP_PO1_g005", xmlNameSpace, xX12855);
XmlNode xLoopPo = xml.SelectSingleNode(xpathLOOP_PO1_g005, xnm);
string[] pO1TArray = { "e01_0354" };
AddChileNode(xml, "PO1", xmlNameSpace, xLoopPo, xpathPO1, xnm, pO1TArray);
}这是我的代码
升成的是这样的<LOOP_PO1_g005>-<PO1><e01_0354>e01_0354</e01_0354><e01_0354>e01_0354</e01_0354><e01_0354>e01_0354</e01_0354><e01_0354>e01_0354</e01_0354></PO1>
<PO1/>
<PO1/>
<PO1/>
</LOOP_PO1_g005>
<LOOP_PO1_g005/>
<LOOP_PO1_g005/>
<LOOP_PO1_g005/>我想问题应该出在
XmlNode xLoopPo = xml.SelectSingleNode(xpathLOOP_PO1_g005, xnm);
这里,但不知道怎么去处理下面是两个方法private void AddChileNode(XmlDocument xml, string nodeName, string xmlNameSpace,
XmlNode xmlNode, string xPath, XmlNamespaceManager xnm,string[] nodeChileName)
{
AddParentNode(xml,nodeName,xmlNameSpace,xmlNode);
XmlNode xChile = xml.SelectSingleNode(xPath, xnm);
XmlElement xEmt = null;
foreach (string node in nodeChileName)
{
xEmt = xml.CreateElement("", node, xmlNameSpace);
xEmt.InnerText = node;
xChile.AppendChild(xEmt);
}
}
private void AddParentNode(XmlDocument xml,string nodeName, string xmlNameSpace,XmlNode xmlNode)
{
XmlElement xmlElement = xml.CreateElement("", nodeName, xmlNameSpace);
xmlNode.AppendChild(xmlElement);
}
XElement carElement = new XElement("Car",
new XAttribute("Id", entity.Id),
new XAttribute("Width", entity.Width),
new XAttribute("Height", entity.Height),
new XAttribute("IsSelected", entity.IsSelected.ToString()),
new XAttribute("CollectionName", entity.CollectionName),
new XAttribute("GroupName", entity.GroupName),
new XElement("Name", entity.Name),
new XElement("IP", entity.IP),
new XElement("X", entity.X),
new XElement("Y", entity.Y)
);
this.xmlDoc.Add(carElement);
他这个是Linq to xml,很方便的对XML进行增删改查。
new XAttribute("Id", entity.Id):是对这个节点增加个属性为Id的
new XElement("IP", entity.IP),:是对这个节点增加个子节点为IP的注意,如果entity.Id,entity.IP为null或“”,请为他赋值,不然为造成<IP/>..