i have a class like
class CXmlData
{
String Data1="";
String Data2="";
List<String> lst=new List<String>()
}
and a Dictionary
Dictionary<String,CXmlData> dict=new Dictionary<String,CXmlData>();
The xml file is having the following structure
<Root>
<ChildList>
<Children>
<Child Name="a1" val="A"/>
<Child Name="a2" val="A"/>
<Child Name="b1" val="B"/>
<Child Name="c1" val="C"/>
<Child Name="c2" val="C"/>
</Childen>
<Siblings_One>
<Sibling Name="A" Xpos="0" Ypos=""/>
<Sibling Name="B" Xpos="1" Ypos="1"/>
</Sibling_One>
<Siblings_Two>
<Sibling Name="C" Xpos="0" Ypos="0"/>
</Sibling_Two>
</ChildList>
</Root>
i need to read the data from the above xml file and to add to the dictionary
so the Resulting Dictionary will be like
Key(String) Value(Instance of CXmlData)
"A" Data1="0"
Data2="2"
lst=new List<String>{"a1","a2"}
"B" Data1="1"
Data2="1"
lst=new List<String>{"b1"}
"C" Data1="0"
Data2="2"
lst=new List<String>{"c1","c2"}
(if Xpos value is "0" then Data2(Ypos) value should be "2") Now I'm using like
1.Read Sibling_One child values
if Xpos value is "0" the take Ypos value as"2"
else keep the same
2.Read Sibling_Two child values
if Xpos value is "0" the take Ypos value as"2"
else keep the same
3.Read Children->Child values
Compare Sibling->Name attribute ==Child->Name
if matches then add the Child->Name attribute value to the list
i'm using Xml Namespace(System.Xml) and foreach loop for this, but its taking more time to complete the process
EDIT Sample Code
XmlDocument XDoc = new XmlDocument();
XDoc.Load(Application.StartupPath + "\\foo.xml");
XmlElement XRoot = XDoc.DocumentElement;
List<string> TempList = new List<string>();
XmlNodeList XChildName = XDoc.SelectNodes("//ChildList/Sibling_One/Sibling");
foreach (XmlNode ch in XChildName)
{
TempList .Add(ch.Attributes["Name"].Value);
CXmlData P = new CXmlData();
if (ch.Attributes["Xpos"].Value == "0")
P.Data2 = "2";
else
P.Data2 = ch.Attributes["Ypos"].Value;
P.Data1 = ch.Attributes["Xpos"].Value;
dict.Add(ch.Attributes["NetName"].Value, P);
}
XChildName = XDoc.SelectNodes("//ChildList/Sibling_Two/Sibling");
foreach (XmlNode ch in XChildName)
{
TempList .Add(ch.Attributes["Name"].Value);
CXmlData P = new CXmlData();
if (ch.Attributes["Xpos"].Value == "0")
P.Data2 = "2";
else
P.Data2 = ch.Attributes["Ypos"].Value;
P.Data1 = ch.Attributes["Xpos"].Value;
dict.Add(ch.Attributes["Name"].Value, P);
}
foreach (string str in TempList )
{
List<String> lstChildValues = (from XmlNode xn in XDoc.SelectNodes("//ChildList/Children/Child")
where xn.Attributes["val"].Value == str
select xn.Attributes["Name"].Value).ToList();
if (dict.Keys.Contains(str))
{
CXmlData p = dict[str];
dict.Remove(str);
p.lst = lstChildValues;
dict.Add(str, p);
}
}
is there any way to do this using LINQ to Xml (System.xml.Linq)
thanks in avance