I've an xml file(Sample.xml) which has the following structure
<Root>
<Child ChildName="Ms_7">
<MissingSiblings>
<Sibling SiblingName="47" />
</MissingSiblings>
</Child>
<Child ChildName="Ms_8">
<MissingSiblings>
<Sibling SiblingName="P2" />
</MissingSiblings>
</Child>
<Child ChildName="Ms_9">
<MissingSiblings>
<Sibling SiblingName="T2" />
</MissingSiblings>
</Child>
<Child ChildName="Ms_10">
<MissingSiblings>
<Sibling SiblingName="R3" />
</MissingSiblings>
</Child>
<Child ChildName="Additional_SIB1">
<AdditionalSiblings>
<Sibling SiblingName="92" />
<Sibling SiblingName="93" />
</AdditionalSiblings>
<MissingSiblings>
<Sibling SiblingName="S3" />
</MissingSiblings>
</Child>
<Child ChildName="Additional_SIB2">
<AdditionalSiblings>
<Sibling SiblingName="39" />
<Sibling SiblingName="34" />
</AdditionalSiblings>
</Child>
</Root>
i need to update another xml file(Result.xml) from the Sample.xml file . like
<Root>
<Step Name="Step1" Date="06/12/2010" Value="">
<Test Name="Ms_7" AdditionalSibling="" MissingSibling="47"/>
<Test Name="Ms_8" AdditionalSibling="" MissingSibling="P2"/>
<Test Name="Ms_9" AdditionalSibling="" MissingSibling="T2"/>
<Test Name="Ms_10" AdditionalSibling="" MissingSibling="R3"/>
<Test Name="SIB1" AdditionalSibling="92,93" MissingSibling="S3"/>
<Test Name="SIB2" AdditionalSibling="39,34" MissingSibling=""/>
</Step>
</Root>
Now i'm using like:
1) iterating through the contents of the Sample.xml file
2) Checking whether the "ChildName" Contains"Additional"
a)Removing temporarly
3) Iterating through child nodes
4) if more than one child appending a comma(,)
the code is as follows:
XmlDocument SampleDoc = new XmlDocument();
XmlDocument ResultDoc = new XmlDocument();
SampleDoc.Load(Application.StartupPath + "\\Sample.xml");
ResultDoc.Load(Application.StartupPath + "\\Result.xml");
XmlElement ResultRoot = ResultDoc.DocumentElement;
XmlElement RsNxt = (XmlElement)ResultDoc.SelectSingleNode("//Step[@Name='Step222']");
if (RsNxt == null)
RsNxt=ResultDoc.CreateElement("Step");
else
RsNxt.RemoveAll();
RsNxt.SetAttribute("Name", "Step1");
RsNxt.SetAttribute("Date", DateTime.Now.ToString());
RsNxt.SetAttribute("Value", "");
String l_strName = "";
XmlNodeList ChildList = SampleDoc.SelectNodes("//Root/Child");
if (ChildList.Count > 0)
{
foreach (XmlNode child in ChildList)
{
XmlElement ChildElement = (XmlElement)child;
l_strName = ChildElement.GetAttribute("ChildName");
bool l_bvalue = l_strName.Contains("Additional");
int l_ntemp = 0;
XmlNodeList l_List = null;
String l_strAdd = "";
String l_strMissing = "";
String l_strAdditional = l_strName;
if (l_bvalue == true)
l_strAdditional = l_strName.Substring(l_strName.IndexOf('_') + 1);
XmlElement Test = ResultDoc.CreateElement("Test");
Test.SetAttribute("Name", l_strAdditional);
if (l_bvalue == false)
{
l_List = ChildElement.SelectNodes("//Child[@ChildName='" + l_strName + "']/MissingSiblings/Sibling");
l_ntemp = 0;
String l_strMissingSibling = "";
foreach (XmlNode l_missingS in l_List)
{
XmlElement Element = (XmlElement)l_missingS;
l_strMissingSibling = Element.GetAttribute("SiblingName");
l_ntemp += 1;
if (l_ntemp == 1)
l_strMissing = l_strMissingSibling;
if (l_ntemp > 1)
{
l_strMissing += ',' + " ";
l_strMissing += l_strMissingSibling;
}
}
}
else
{
l_List = ChildElement.SelectNodes("//Child[@ChildName='" + l_strName +"']/AdditionalSiblings/Sibling");
l_ntemp = 0;
String l_strAddSibling = "";
foreach (XmlNode Additional in l_List)
{
XmlElement nodeElement = (XmlElement)Additional;
l_strAddSibling = nodeElement.GetAttribute("SiblingName");
l_ntemp += 1;
if (l_ntemp == 1)
l_strAdd = l_strAddSibling;
if (l_ntemp > 1)
{
l_strAdd += ',' + " ";
l_strAdd += l_strAddSibling;
}
}
}
Test.SetAttribute("AdditionalSiblings", l_strAdd);
Test.SetAttribute("MissingSiblings", l_strMissing);
RsNxt.AppendChild(Test);
}
}
ResultRoot.AppendChild(RsNxt);
ResultDoc.Save(Application.StartupPath + "\\Result.xml");
Is there any other way to do this like Xml to Linq approch?
Thanks in Advance