xml文件如下:<books>
<cnbook>aa</cnbook>
<cnbook>bb</cnbook>
<cnbook>cc</cnbook>
<enbook>xx</enbook>
<enbook>yy</enbook>
<enbook>zz</enbook>
</books>
就样的xml应该如何读取?

解决方案 »

  1.   

    Sample code as follows:
    string strValue = @"<books>
    <cnbook>aa</cnbook>
    <cnbook>bb</cnbook>
    <cnbook>cc</cnbook>
    <enbook>xx</enbook>
    <enbook>yy</enbook>
    <enbook>zz</enbook>
    </books>";NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);//Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Default);XmlTextReader reader = new XmlTextReader( strValue , XmlNodeType.Element, context);
    reader.WhitespaceHandling = WhitespaceHandling.None;//Parse the XML and display each of the nodes.
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element:
    Debug.WriteLine( reader.Name );
    break;
    case XmlNodeType.Text:
    Debug.WriteLine( reader.Value );
    break;
    case XmlNodeType.EndElement:
    Debug.WriteLine( reader.Name );
    break;
    }
    }
    reader.Close();
      

  2.   

    你要想看值,把output窗口打开就是了,如果想用弹出窗口,把
    Debug.WriteLine( reader.Value );
    改为
    MessageBox.Show( reader.Value );
    即可。