开发者

XML file conversion issue

开发者 https://www.devze.com 2022-12-08 19:57 出处:网络
I am using VSTS 2008 + C# + .Net 3.5 to conversion the below input XML file into destination format. There may be arbitrary number of Image elements. And the new Price information is provided in List.

I am using VSTS 2008 + C# + .Net 3.5 to conversion the below input XML file into destination format. There may be arbitrary number of Image elements. And the new Price information is provided in List.

Any quick way to implement the conversion function?

source format,

<?xml version="1.0"?>
<Metadata ve开发者_运维知识库rsion="1">
  <Owner>George</Owner>
  <Image>
    <x>100</x>
  </Image>
  <Image>
    <x>200</x>
  </Image>
</Metadata>

destination format,

<?xml version="1.0"?>
<Metadata version="1">
  <Owner>George</Owner>
  <Image>
    <x>100</x>
    <Price>200</Price>
  </Image>
  <Image>
    <x>100</x>
    <Price>300</Price>
  </Image>
</Metadata>


Quite simply: select all <Image> nodes and add a new <Price> child node to them:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("your-filename-here.xml");

XmlNodeList imageList = xmldoc.SelectNodes("/Metadata/Image");

foreach(XmlNode node in imageList)
{
   XmlElement priceElement = xmldoc.CreateElement("Price");
   priceElement.InnerText = "300";  // or whatever it is

   node.AppendChild(priceElement);
}

xmldoc.Save("your-new-xml-file-name-here.xml");

This should do it, I hope!

Marc

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号