使用 Linq 创建 xml 的更有效方法
我通过采用 skelton xml 并用值填充节点来创建 xml,如果需要,可以在 xml 之间添加新节点。我正在按照以下方式做,感觉...效率不高。 有没有更好的方法来满足我的要求?
示例普通产品 Xml:
<root>
<ProductInformation>
<Name></Name>
<ProductId></ProductId>
<Description></Description>
<Details>
<TabName></TabName>
<DetailList>
<Add>
<Type></Type>
<Information></Information>
</Add>
</DetailList>
</Details>
</ProductInformation>
</root>
代码:
XDocument xmlDoc = XDocument.Load(Server.MapPath("/PlainProduct.xml"));
xmlDoc.Element("root").Element("ProductInformation").SetElementValue("Name","iPhone");
xmlDoc.Element("root").Element("ProductInformation").SetElementValue("ProductId", "123456");
foreach (XElement detail in xmlDoc.Descendants("ProductInformation").Elements("Details"))
{
detail.SetElementValue("TabName", "Specifications");
foreach (XElement x in detail.Elements("DetailList"))
{
//Update node value and here the node already exists in plain skelton xml
x.Element("Add").SetElementValue("Type","Plastic");
x.Element("Add").SetElementValue("Information", "24 Carat");
//Added below two new nodes dynamically if my backend has more items of details programmatically
x.Add(new XElement("Add",
new XElement("Type", "Fabric"),
new XElement("Information", "Smooth")));
x.Add(new XElement("Add",
new XElement("Type", "Finishes"),
new XElement("Information", "Rogh")));
}
}
I am creating xml by taking skelton xml and filling the nodes with values and if needed adding new nodes in between xml. I am doing in the following way and felt like ...not efficient.
Is there any better way of doing my requirement?
Sample Plain Product Xml:
<root>
<ProductInformation>
<Name></Name>
<ProductId></ProductId>
<Description></Description>
<Details>
<TabName></TabName>
<DetailList>
<Add>
<Type></Type>
<Information></Information>
</Add>
</DetailList>
</Details>
</ProductInformation>
</root>
Code:
XDocument xmlDoc = XDocument.Load(Server.MapPath("/PlainProduct.xml"));
xmlDoc.Element("root").Element("ProductInformation").SetElementValue("Name","iPhone");
xmlDoc.Element("root").Element("ProductInformation").SetElementValue("ProductId", "123456");
foreach (XElement detail in xmlDoc.Descendants("ProductInformation").Elements("Details"))
{
detail.SetElementValue("TabName", "Specifications");
foreach (XElement x in detail.Elements("DetailList"))
{
//Update node value and here the node already exists in plain skelton xml
x.Element("Add").SetElementValue("Type","Plastic");
x.Element("Add").SetElementValue("Information", "24 Carat");
//Added below two new nodes dynamically if my backend has more items of details programmatically
x.Add(new XElement("Add",
new XElement("Type", "Fabric"),
new XElement("Information", "Smooth")));
x.Add(new XElement("Add",
new XElement("Type", "Finishes"),
new XElement("Information", "Rogh")));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“高效”是指“更易于维护”,您可能想尝试这种技术:
...然后您这样做
If by "efficient", you mean "easier to maintain", you may want to try this technique:
...and then you do this