属性值读?

时间:2017-04-09 22:31:18

标签: c# .net xml

目前我试图读取没有成功的XML属性,到目前为止我得到的是:

        XmlDocument _doc = new XmlDocument();
        _doc.LoadXml(@"<DATA jsonclass=""array"">
        <ROW id=""1"">
          <D n=""697""/>
          <D n=""2601"">10815</D>
          <D n=""3242""/>
          <D n=""3243"">2017-03-15 00:00:00.0</D>
          <D n=""3245"">+</D>
          <D n=""3274""/>
          <D n=""4895"">USD</D>
        </ROW>
        <ROW id=""1"">
          <D n=""697""/>
          <D n=""2601"">10816</D>
          <D n=""3242""/>
          <D n=""3243"">2017-03-15 00:00:00.0</D>
          <D n=""3245"">+</D>
          <D n=""3274""/>
          <D n=""4895"">USD</D>
        </ROW>
      </DATA>");


        XmlNodeList elemLista = _doc.GetElementsByTagName("D");
        for (int i = 0; i < elemLista.Count; i++)
        {
            string attrVal = elemLista[i].Attributes["n"].Value;

            //this is returnig 2601 and Im looking for **10816**
            Console.WriteLine(attrVal);

            //      Req.RequestAllPOAvailable();`



        }
    }

我期望得到10815和10816,但结果是一样的...输出仍然是2601,我做错了什么?谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

你获得2601的原因是你获得了获得元素内部文本所需的n属性的值:

 XmlDocument _doc = new XmlDocument();
        _doc.LoadXml(@"<DATA jsonclass=""array"">
        <ROW id=""1"">
          <D n=""697""/>
          <D n=""2601"">10815</D>
          <D n=""3242""/>
          <D n=""3243"">2017-03-15 00:00:00.0</D>
          <D n=""3245"">+</D>
          <D n=""3274""/>
          <D n=""4895"">USD</D>
        </ROW>
        <ROW id=""1"">
          <D n=""697""/>
          <D n=""2601"">10816</D>
          <D n=""3242""/>
          <D n=""3243"">2017-03-15 00:00:00.0</D>
          <D n=""3245"">+</D>
          <D n=""3274""/>
          <D n=""4895"">USD</D>
        </ROW>
      </DATA>");


        XmlNodeList elemLista = _doc.GetElementsByTagName("D");
        for (int i = 0; i < elemLista.Count; i++)
        {
            string attrVal = elemLista[i].InnerText;

            //this is returnig 2601 and Im looking for **10816**
            Console.WriteLine(attrVal);

            //      Req.RequestAllPOAvailable();`



        }