Read XML in C#
-
hello guys, I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver Example <Root> <Data id="1" Name="aaa" Phone="852963" Gender="Male" /> <Data id="2" Name="bbb" Phone="123456789" Gender="Female" /> <Data id="3" Name="ccc" Phone="987654" Gender="Male" /> <Data id="3" Name="ddd" Phone="7676767" Gender="Male" /> </Root> i need to read the first(TOP)id record and who's Gender="male". how to do this Regards Shafiq
-
hello guys, I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver Example <Root> <Data id="1" Name="aaa" Phone="852963" Gender="Male" /> <Data id="2" Name="bbb" Phone="123456789" Gender="Female" /> <Data id="3" Name="ccc" Phone="987654" Gender="Male" /> <Data id="3" Name="ddd" Phone="7676767" Gender="Male" /> </Root> i need to read the first(TOP)id record and who's Gender="male". how to do this Regards Shafiq
Try Bipin Joshi's book "Beginning XML with C# 2008" by Apress. I think thats there. ~GER
Ger
-
hello guys, I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver Example <Root> <Data id="1" Name="aaa" Phone="852963" Gender="Male" /> <Data id="2" Name="bbb" Phone="123456789" Gender="Female" /> <Data id="3" Name="ccc" Phone="987654" Gender="Male" /> <Data id="3" Name="ddd" Phone="7676767" Gender="Male" /> </Root> i need to read the first(TOP)id record and who's Gender="male". how to do this Regards Shafiq
SelectNodes and XPath.
-
hello guys, I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver Example <Root> <Data id="1" Name="aaa" Phone="852963" Gender="Male" /> <Data id="2" Name="bbb" Phone="123456789" Gender="Female" /> <Data id="3" Name="ccc" Phone="987654" Gender="Male" /> <Data id="3" Name="ddd" Phone="7676767" Gender="Male" /> </Root> i need to read the first(TOP)id record and who's Gender="male". how to do this Regards Shafiq
Here is the Answer!
XmlDocument xmlDoc =new XmlDocument;
xmlDoc.Load("C:\MyXml");
string myXPath="/Root/Data[@Gender='Male']"; //so with this you can get only male nodesXmlNodeList NodeList=xmlDoc.SelectNodes(myXPath); //Selects all the male nodes
XmlNode xmlNod=NodeList(0); //The first male node
Int myId=xmlNod.Attributes("id").Value; //Returns the id of the first Male node
Cheers! :)
-
Here is the Answer!
XmlDocument xmlDoc =new XmlDocument;
xmlDoc.Load("C:\MyXml");
string myXPath="/Root/Data[@Gender='Male']"; //so with this you can get only male nodesXmlNodeList NodeList=xmlDoc.SelectNodes(myXPath); //Selects all the male nodes
XmlNode xmlNod=NodeList(0); //The first male node
Int myId=xmlNod.Attributes("id").Value; //Returns the id of the first Male node
Cheers! :)
-
hello guys, I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver Example <Root> <Data id="1" Name="aaa" Phone="852963" Gender="Male" /> <Data id="2" Name="bbb" Phone="123456789" Gender="Female" /> <Data id="3" Name="ccc" Phone="987654" Gender="Male" /> <Data id="3" Name="ddd" Phone="7676767" Gender="Male" /> </Root> i need to read the first(TOP)id record and who's Gender="male". how to do this Regards Shafiq
XPath is easy to use, uses a hierarchical syntax and is available in many languages. LINQ is far more flexible, but is .NET-centric.