How can I read this using C#. Please help me in this. which will be easiest way for reading it? I tried to read it into a DataSet but its showing some error.
First sorry to say that your xml contents is something wrong……:Without namespace——What's the defination of "ss"?
Then your xml cotents cannot be changed to a normal DataTable/DataSet,not every xml contents can do that。You can refer the sample that can converted to DataSet/DataTable by using ReadXml/WriteXml:
To your situation,I think you can try this——let's support that your xml namespace is well-formatted,and then use XmlNameSpaceManager to add the specific namespace for the xml contents,Here's a full sample for you to refer:
smilu.net
Member
157 Points
84 Posts
How to read XML in C#?
May 07, 2012 04:48 AM|LINK
I have an XML file which looks like this
<ss:demo> <ss:Name> <ss:FirstName>First</ss:FirstName> <ss:SecondName>Second</ss:SecondName> </ss:Name> <ss:country code=IN>India</ss:country> </ss:demo>How can I read this using C#. Please help me in this. which will be easiest way for reading it? I tried to read it into a DataSet but its showing some error.
tusharrs
Contributor
3230 Points
668 Posts
Re: How to read XML in C#?
May 07, 2012 04:54 AM|LINK
http://msdn.microsoft.com/en-us/library/ekw4dh3f.aspx
if still you are getting the following error
'ss' is an undeclared prefix. Line 2, position 2.
then refer to this link http://stackoverflow.com/questions/2811053/undeclared-reference-to-namespace-prefix-error
and i think you have to declare ss
( Mark as Answer if it helps you out )
View my Blog
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to read XML in C#?
May 09, 2012 01:18 AM|LINK
First sorry to say that your xml contents is something wrong……:Without namespace——What's the defination of "ss"?
Then your xml cotents cannot be changed to a normal DataTable/DataSet,not every xml contents can do that。You can refer the sample that can converted to DataSet/DataTable by using ReadXml/WriteXml:
【Sample】
http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to read XML in C#?
May 09, 2012 01:30 AM|LINK
To your situation,I think you can try this——let's support that your xml namespace is well-formatted,and then use XmlNameSpaceManager to add the specific namespace for the xml contents,Here's a full sample for you to refer:
XmlDocument doc = new XmlDocument(); doc.Load("XMLFile1.xml"); XmlNamespaceManager nmg = new XmlNamespaceManager(doc.NameTable); nmg.AddNamespace("ss", "http://schemas.xmlsoap.org/wsdl/"); XmlElement ele = (XmlElement)doc.SelectSingleNode("//ss:FirstName", nmg); Console.WriteLine(ele.InnerText); XmlElement ele2 = (XmlElement)doc.SelectSingleNode("//ss:SecondName", nmg); Console.WriteLine(ele2.InnerText); XmlElement ele3 = (XmlElement)doc.SelectSingleNode("//ss:country", nmg); Console.WriteLine(ele3.InnerText);【Your xml】
<root xmlns:ss="http://schemas.xmlsoap.org/wsdl/"> <ss:demo> <ss:Name> <ss:FirstName>First</ss:FirstName> <ss:SecondName>Second</ss:SecondName> </ss:Name> <ss:country code="IN">India</ss:country> </ss:demo> </root>