XML file conversion issue
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
精彩评论