asp.net 2.0 如何读取xml字符串
我在程序里生成了一串(类似 " <a> <b> ddd </b> </a> ")字符串,我想接着读取出他的节点,只找到读取xml文件的方法,但是我没有看到有读取xml字符串的方法?帮忙....不胜感激!
------解决方案--------------------XmlTextReader的构造函数有传入字符串的重载
//例子
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XML fragment to be parsed.
string xmlFrag = " <book> " +
" <title> Pride And Prejudice </title> " +
" <bk:genre> novel </bk:genre> " +
" </book> ";
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace( "bk ", "urn:sample ");
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
//Create the reader.
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
//Parse the XML. If they exist, display the prefix and
//namespace URI of each element.
while (reader.Read()){
if (reader.IsStartElement()){
if (reader.Prefix==String.Empty)
Console.WriteLine( " <{0}> ", reader.LocalName);
else{
Console.Write( " <{0}:{1}> ", reader.Prefix, reader.LocalName);
Console.WriteLine( " The namespace URI is " + reader.NamespaceURI);
}
}
}
//Close the reader.
reader.Close();
}
}
------解决方案--------------------这个XML读取和输出的格式,应该遵循什么样的原则啊?