比如以下xml
<books>
<book>
   <title>XML</title>
   <publisher>wrox</Publisher>
<book>
   <publisher>tsinghua</publisher>
</book>
<book>
   <Title>C# framework</title>
</book>
<books>用下面的程序读
 XmlNodeList nodes7 = doc.SelectNodes(@"books/book");
 for (int i = 0; i < nodes7.Count; i++)
    {
    if (nodes7[i].HasChildNodes)
    {
     for (int m = 0; m < nodes7[i].ChildNodes.Count; m++)
    {
       if (nodes7[i].ChildNodes[m].Name == "title")
    {
    strtitle= nodes7[i].ChildNodes[m].InnerText;
                                            
     }
if (nodes7[i].ChildNodes[m].Name == "publisher")
    {
    strpublisher= nodes7[i].ChildNodes[m].InnerText;
                                            
     }
}
}第二个book没有title节点,但他以第一个wrox在读,第三个book没有publisher,但输出的却是第二个tsinghua.我想以book循环读取book下的每一个节点并赋值给变量,应该如何去做????

解决方案 »

  1.   

    像你这样的数据,我建议你用xmlelement去逐个分析。
      

  2.   

    能给我举个例子吗?我没用过xmlelement.给个示例可以吗
      

  3.   

    使用xpath来处理。
    http://www.cnblogs.com/thinhunan/archive/2006/10/11/525947.html
      

  4.   

    brother, it's not resulted from reading but storing. you defined variable 'strpublisher' outside the for loop. when you read the second node, you stored 'tsinghua' to variable 'strpublisher '.You didn't assign value for it when you read the third node,right? So the value of variable "strpublisher" is still 'tsinghua'. I think you know how to fix it.
      

  5.   

    By the way,xmlelement is an xmlnode too. but the way to use is a little different. For example, you can define it:
    XmlElement xe = XmlElement(node) ;
    OR 
    XmlElement xe = node as XmlElement ;
    Then an XmlElement-based object is defined. sometimes,XmlElement is easy to use. If you want to get a attribute value of this node, you can use like this: xe.GetAttribute("publisher"). 
    But for XmlNode, you should use like : node.Attributes["publisher"]
      

  6.   

    你的程序逻辑有问题 。
    if()
    需要
    else
      

  7.   

    string xml = @"
    <books>
    <book>
       <title>XML</title>
       <publisher>wrox</publisher>
    </book>   
    <book>
       <publisher>tsinghua</publisher>
    </book>
    <book>
       <title>C# framework</title>
    </book>
    </books>";
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.LoadXml(xml);
        System.Xml.XmlNodeList nodes = doc.SelectNodes("/books/book");
        if (nodes != null)
        {      string[] strTitle = new string[nodes.Count];
          string[] strPublisher = new string[nodes.Count];
          
          for (int i = 0 ; i < nodes.Count ; i++)
          {
            System.Xml.XmlNode titleNode = nodes[i].SelectSingleNode("title");
            if (titleNode == null)
            {
              strTitle[i] = "";
            }
            else
            {
              strTitle[i] = titleNode.InnerText;
            }        System.Xml.XmlNode publisherNode = nodes[i].SelectSingleNode("publisher");
            if (publisherNode == null)
            {
              strPublisher[i] = "";
            }
            else
            {
              strPublisher[i] = publisherNode.InnerText;
            }          
         }
          for (int i = 0 ; i < strTitle.Length ; i++)
          {
            Response.Write("<li>" + strTitle[i]);
            
          }
          Response.Write("<hr>");
          for (int i = 0 ; i < strPublisher.Length ; i++)
          {
            Response.Write("<li>" + strPublisher[i]);
          }
         
        
        }
      

  8.   

    你给的xml样例结构有问题, 
    用xmltextreader吧, 在控制上比较好
      

  9.   

    如果有两个title,按孟子的方法我每次只能读第一个,这应该怎么解决.还有title的属性是用attribute这个属性取出吗??<books>
    <book>
       <title id="01" author="AA">XML</title>
       <title>VB.net</title>
       <publisher>wrox</Publisher>
    <book>
       <publisher>tsinghua</publisher>
    </book>
    <book>
       <Title>C# framework</title>
       <Title>Dotnet framework</title>
    </book>
    <books>
      

  10.   

    SelectSingleNode就是取一个
    "@author"就是属性