从字符串解析XML

时间:2010-04-14 07:33:45

标签: c# xml

我有一个xml字符串:

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

如何使用XMLReader类解析XML以将“this is the result”作为字符串返回。

thanx!

2 个答案:

答案 0 :(得分: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());
   }
}

答案 1 :(得分:1)

只需使用该字符串创建一个xml阅读器并将其用于解析

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