1

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.

4
  • 1
    What error is it showing, and what do you want to do with the XML after you have read it in? Commented May 7, 2012 at 4:47
  • What code did you use to read it? Commented May 7, 2012 at 4:47
  • I can see two XML errors: no namespace declaration and IN isn't in quotes in the code attribute. My answer expects the second to be fixed and works regardless of the first. Commented May 7, 2012 at 4:52
  • I have only written the inner code. everything is there in it. And also "" is also there. When i wrote here i wrote the mistake. Commented May 7, 2012 at 4:54

2 Answers 2

6

There are several strategies to read an XML document, or parts thereof, using C#. If you are more specific about what you want to do with the XML document, the community can provide you with more specific guidance.

Here are some top choices:

Linq to XML

http://msdn.microsoft.com/en-us/library/bb387098.aspx

http://msdn.microsoft.com/en-us/library/bb387065.aspx

XDocument (part of the Linq to XML framework)

http://msdn.microsoft.com/en-us/library/bb387063.aspx

XmlDocument

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx

XPath

http://msdn.microsoft.com/en-us/library/ms256471.aspx

Sign up to request clarification or add additional context in comments.

Comments

3

How about just LINQ to XML?

Given this:

var xml = "<ss:demo>\r\n<ss:Name>\r\n    <ss:FirstName>First</ss:FirstName>\r\n    <ss:SecondName>" +
"Second</ss:SecondName>\r\n</ss:Name>\r\n<ss:country code=\"IN\">India</ss:country>\r\n</ss" +
":demo>";

(Note I had to wrap IN in quotes as such "IN")

Declare some namespaces and read it in:

var mngr = new XmlNamespaceManager( new NameTable() );
mngr.AddNamespace( "ss", "urn:ignore" ); // or proper URL
var parserContext = new XmlParserContext(null, mngr, null, XmlSpace.None, null);

If reading from a string as above:

var txtReader = new XmlTextReader( xml, XmlNodeType.Element, parserContext );

If reading from a file instead:

var txtReader = new XmlTextReader( new FileStream(filename, FileMode.Open), XmlNodeType.Element, parserContext );

Then load:

var ele = XElement.Load( txtReader );

ele contains everything you need.

2 Comments

You meant XDocument.Load in this case, right? (downvote was not from me)
Yes it's a file, but to show an example I used a string. I've updated to include what it would be like with a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.