0

我有一个这样的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”节点。

4

3 回答 3

1

你应该看看这里的XDocument课程:http: //msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx

几周前我不得不做一些与此非常相似的事情。XDocument 更愿意操作数据,并且与它一起使用 LINQ 非常容易。

于 2012-08-08T15:16:46.867 回答
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";
        }

此外,此文档是 Xpath 的一个非常方便的参考

于 2012-08-08T15:18:55.660 回答
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());
于 2012-08-08T16:42:27.503 回答