1

I have a xml string:

<Test> Result : this is the result</Test>

How do i parse XML using XMLReader class to get "this is the result" as a string back.

thanx !

8
  • This is not XML. XML requires a header. Commented Apr 14, 2010 at 7:39
  • sorry i didnt put in the xml string correctly. "<Test> Result : This is the result</Test>" Commented Apr 14, 2010 at 7:42
  • XML fragments do not require a header, and xmlreader can quite happily read it, header or not. Commented Apr 14, 2010 at 8:01
  • Because it works doesn't necessarily mean it is correct ;) Without a header, you have no information about the encoding and cannot reliabily parse XML. However, I guess one can deviate from the standard in some simple cases. I just don't like it. Commented Apr 14, 2010 at 8:08
  • 1
    @Richard Sure, but SHOULD != CAN as well ;) For example: "You SHOULD wear clothes" => it is not mandatory but still recommended. Well, my example is stupid, but it is mostly a matter of interpretation. I always try to enforce the theory, but I can understand that things differ in practice. In this case, I think it is wrong to omit the header, but again, it is only my opinion. And our discussion has become "off-topic" I guess. Commented Apr 14, 2010 at 11:40

2 Answers 2

3
var r = System.Xml.XmlReader.Create(new System.IO.StringReader("<Test> Result : this is the result</Test>"))
while (r.Read()) {
   if (r.NodeType == XmlNodeType.Element && r.LocalName == "Test") {
     Console.Write(r.ReadElementContentAsString());
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

works, just replace <xmlstring> by "<Test> Result : this is the result</Test>"
1

Just create an xml reader using that string and use it for parsing

var reader = System.Xml.XmlReader.Create(new System.IO.StringReader(<xmlstring>))

2 Comments

when i do the "using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))" the reader says there are no attributes. Can some1 give me an example please?
@Dror Helper: You need to expand your answer to actually answer the "How do i parse XML using XMLReader class to get "this is the result" as a string back." bit. You've only really shown a hint of the way to go, not a solution.

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.