我正在从 sql 数据库加载记录并将它们插入到 XML 树中,但是在我想要它们的列表关闭后插入新记录。
我曾尝试使用 addafterself 和 addbeforeself 这两种方法都会导致空引用异常。
这是我用来填充 xml 的函数
private void FillXml(List<ClientCrosswalk> clients)
{
XElement doc = XElement.Load(cFolder + @"\Files\emptyClientXMLMod.xml");
List<XElement> elements = new List<XElement>();
//Add the first client to the list to avoid overwriting
foreach (var node in doc.Descendants("ClientLocation"))
{
elements.Add(node);
node.Element("Description").Value = clients[0].AcctNo.ToString() + " " + clients[0].AcctName.ToString();
node.Element("LocationName").Value = clients[0].DivCode.ToString();
}
clients.RemoveAt(0);
doc.Save(cFolder + @"\Outbox\clientSIE.xml");
//Build a new xml tree for each respective client and add it to the Client list
foreach (var client in clients)
{
foreach (var elm in elements)
{
elm.Element("Description").Value = client.AcctNo.ToString() + " " + client.AcctName.ToString();
elm.Element("LocationName").Value = client.DivCode.ToString();
doc.Add(elm);
}
}
doc.Save(cFolder + @"\Outbox\clientSIE.xml");
}
我期待生成的 xml 是
<Client>
<ClientLocationsList>
<ClientLocation>
data set1
</ClientLocation>
<ClientLocation>
data set2
</ClientLocation>
</ClientLocationsList>
</Client>
相反,我得到的是
<ClientLocationsList>
<ClientLocation>
data set1
</ClientLocation>
</ClientLocationsList>
</Client>
data set2
我的同事能够帮助我,也许这会帮助其他人
我变了
changeDoc.Add(elm);
到
changeDoc.Descendants("Client").First().Element("ClientLocationsList").Add(elm);
问题只是使用 add 是告诉程序添加没有父级的指定对象,更新的方式告诉他们我想插入到客户端的子级 ClienLocationsList 的第一个实例中
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句