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 !

Jens
  • 25,229
  • 9
  • 75
  • 117
no9
  • 6,424
  • 25
  • 76
  • 115
  • This is *not* XML. XML requires a header. – ereOn Apr 14 '10 at 07:39
  • sorry i didnt put in the xml string correctly. " Result : This is the result" – no9 Apr 14 '10 at 07:42
  • XML fragments do not require a header, and xmlreader can quite happily read it, header or not. – dnolan Apr 14 '10 at 08: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. – ereOn Apr 14 '10 at 08:08
  • @ereOn: There is no need to specify the encoding, all .NET strings are UTF-16 (they cannot be anything else). Encoding is only meaningful when working from an octet stream. – Richard Apr 14 '10 at 08:52
  • @Richard I totally agree on a technical level. However, the header also contains the XML version. Not specifying it might lead to ambiguity in the future. Quoting from the XML RFC: [Definition: XML documents SHOULD begin with an XML declaration which specifies the version of XML being used.] – ereOn Apr 14 '10 at 09:38
  • @ereOn: "SHOULD" != "MUST". In practice this has never been an issue (XML 1.1 has ended up like XHTML 2: ignored), and for an example gets in the way. But feel to pre-pend XML declarations onto *your* strings. – Richard Apr 14 '10 at 10:05
  • 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. – ereOn Apr 14 '10 at 11:40

2 Answers2

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());
   }
}
dnolan
  • 2,349
  • 19
  • 21
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>))
Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • 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? – no9 Apr 14 '10 at 08:06
  • @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. – Rob Levine Apr 14 '10 at 08:08