我有一个像这样的xml文件,
<?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>
我想像下面那样修改它,
<?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>
所有新节点和值都将通过C#函数添加。我们现在将这些新值假设为静态值。
节点“FeaturedProduct”也不仅仅是一个。该名称中有很多节点。我想修改所有“FeaturedProduct”节点。
答案 0 :(得分:1)
您应该在这里查看XDocument
课程:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
答案 1 :(得分:0)
这不完全使用linq,但它将满足您的目的
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";
}
此外,This Documentation是Xpath
的非常方便的参考答案 2 :(得分:0)
以下是如何使用Linq:
string documentXml = @"<?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>";
string productsXml = @"<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>";
XDocument document = XDocument.Parse(documentXml);
var targetNodes = from featuredProduct in document.Descendants("FeaturedProduct")
from contentSelector in featuredProduct.Elements("ContentSelector")
select contentSelector;
foreach (var targetNode in targetNodes)
{
targetNode.ReplaceWith(XElement.Parse(productsXml));
}
Console.WriteLine(document.ToString());