添加节点,并使用C#/ LINQ的中的XML元素 [英] Add node and elements in xml using C#/Linq
本文介绍了添加节点,并使用C#/ LINQ的中的XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样一个XML文件,
<?XML版本=1.0编码=UTF-8&GT?;
<根和GT;
< FeaturedProductCategories>
< FeaturedProductCategory>
<&特色产品GT;
< FeaturedProduct>
< ContentSelector datavalue_idtype =内容:智能表datavalue_displayvalue =«智能表:49»> 49 LT; / ContentSelector>
< / FeaturedProduct>
< /&特色产品GT;
< / FeaturedProductCategory>
< / FeaturedProductCategories>
< /根>
我想修改它类似下面,
<?XML版本=1.0编码=UTF-8&GT?;
<根和GT;
<标题>首页广告和LT; /标题>
< FeaturedProductCategories>
< FeaturedProductCategory>
<&特色产品GT;
< FeaturedProduct>
<产品>
<产品与GT;
< ProductTitle>产品与LT; / ProductTitle>
&所述; ProductDate> 2012年3月8日11:57:25℃/ ProductDate>
< ProductImage>
&所述; IMG SRC =ex1.jpg/>
< / ProductImage>
< ProductThumbnailImage>
&所述; IMG SRC =ex2.jpg/>
< / ProductThumbnailImage>
< ProductCaption>产品&字幕LT; / ProductCaption>
< ProductImage>
&所述; IMG SRC =ex3.jpg/>
< / ProductImage>
< ProductThumbnailImage>
&所述; IMG SRC =ex4.jpg/>
< / ProductThumbnailImage>
< ProductCaption>产品&字幕LT; / ProductCaption>
< /产品与GT;
< /产品>
< / FeaturedProduct>
< /&特色产品GT;
< / FeaturedProductCategory>
< / FeaturedProductCategories>
< /根>
所有的新节点和值是通过一个C#功能被添加。让我们假定这些新值静态值了。
另外,节点FeaturedProduct不是唯一的一个。有很多在该名称的节点。我想修改所有FeaturedProduct节点。
解决方案
这不使用LINQ准确,但它会成为你的目的。
XDOC XmlDocument的=新的XmlDocument();
xDoc.Load(filename.xml中); 的foreach(XmlNode的xNode在xDoc.SelectNodes(// FeaturedProduct))
{
的XmlElement为newElement = xDoc.CreateElement(newElementName);
XmlAttribute newAttribute = xDoc.CreateAttribute(为AttributeName);
newAttribute.Value =的AttributeValue;
newElement.Attributes.Append(newAttribute); xNode.AppendChild(为newElement);
xNode.InnerText =myInnerText;
}
此外,本文档是用于XPath <一个非常方便的参考/ p>
I have an xml file like this,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<ContentSelector datavalue_idtype="content:smartform" datavalue_displayvalue="«Smart Form:49»">49</ContentSelector>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
I want to modify it like the one below,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Title>HomePage</Title>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<Products>
<Product>
<ProductTitle>Product</ProductTitle>
<ProductDate>03-08-2012 11:57:25</ProductDate>
<ProductImage>
<img src="ex1.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex2.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
<ProductImage>
<img src="ex3.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex4.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
</Product>
</Products>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
All the new nodes and values are to be added through a C# function. Let us assume these new values as static values for now.
Also the node "FeaturedProduct" is not only one. There are a lot of nodes in that name. I want to modify all the "FeaturedProduct" nodes.
解决方案
This doesn't use linq exactly, but It will serve your purpose
XmlDocument xDoc = new XmlDocument();
xDoc.Load("filename.xml");
foreach (XmlNode xNode in xDoc.SelectNodes("//FeaturedProduct"))
{
XmlElement newElement = xDoc.CreateElement("newElementName");
XmlAttribute newAttribute = xDoc.CreateAttribute("AttributeName");
newAttribute.Value = "attributeValue";
newElement.Attributes.Append(newAttribute);
xNode.AppendChild(newElement);
xNode.InnerText = "myInnerText";
}
Also, This Documentation is a very handy reference for Xpath
这篇关于添加节点,并使用C#/ LINQ的中的XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文