阅读所有同级节点元素

时间:2014-05-16 20:24:33

标签: c# xml

我的情况是我有一个xml文件,如下所示:

<initialize>
 <boxes>
  <box>
     <compliment>RayTest1</compliment>
     <description>RayTest1</description>
     <boxtype>RayTest</boxtype>
     <il>1.00</il>
     <ib>1.00</ib>
     <ih>1.00</ih>
     <ol>1.00</ol>
     <ob>1.00</ob>
     <oh>1.00</oh>
     <bw>1.00</bw>
     <bgw>1.00</bgw>
     <boxstrength>1</boxstrength>
  </box>
  <box>
     <compliment>RayTest2</compliment>
     <description>RayTest2</description>
     <boxtype>RayTest</boxtype>
     <il>99.99</il>
     <ib>99.99</ib>
     <ih>99.99</ih>
     <ol>99.99</ol>
     <ob>99.99</ob>
     <oh>99.99</oh>
     <bw>99.99</bw>
     <bgw>99.99</bgw>
     <boxstrength>99</boxstrength>
  </box>
 </boxes>
</initialize>

我需要在C#中完成的是:

用户要将xml文件上传到我的页面。

我需要解析xml文件的每个框,将值赋给对象,然后将该对象添加到列表中以便在ASP.NET MVC App中显示。

我试图通过以下方式做到这一点:

                try
                {
                    List<BoxViewModel> boxesList = new List<BoxViewModel>();
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(fileFromPage.FileName);

                    XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;

                    foreach (XmlNode boxNode in boxNodeList)
                    {
                        XmlNode currentBoxNode = boxNode;
                        BoxViewModel currentBox = new BoxViewModel();

                        currentBox.Compliment = currentBoxNode.SelectSingleNode("//compliment").InnerText;
                        currentBox.Description = currentBoxNode.SelectSingleNode("//description").InnerText;
                        currentBox.BoxType = currentBoxNode.SelectSingleNode("//boxtype").InnerText;
                        currentBox.InsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("//il").InnerText);
                        currentBox.InsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("//ib").InnerText);
                        currentBox.InsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("//ih").InnerText);
                        currentBox.OutsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("//ol").InnerText);
                        currentBox.OutsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("//ob").InnerText);
                        currentBox.OutsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("//oh").InnerText);
                        currentBox.BoxWeight = decimal.Parse(currentBoxNode.SelectSingleNode("//bw").InnerText);
                        currentBox.BoxGrossWeight = decimal.Parse(currentBoxNode.SelectSingleNode("//bgw").InnerText);
                        currentBox.BoxStrength = int.Parse(currentBoxNode.SelectSingleNode("//boxstrength").InnerText);

                        boxesList.Add(currentBox);
                    }

                }
                catch
                {
                    ViewBag.MsgText = "There was an error when processing the file!";
                    ViewBag.MsgColor = "Red";
                }

我在这里遇到的问题是,当我这样做时,每个盒子都是一样的。它只获得第一个盒子。 foreach循环足够智能,知道有两个框,但它无法获取第二个框并将其添加到我的列表中。相反,我有两个相同框的列表。

我愿意改变这方面的任何事情以使其发挥作用。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

而不是使用

XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;

尝试使用:

XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");

这应该为您提供每个盒子节点的节点列表。

答案 1 :(得分:1)

您正在引用XML文档的根,这就是为什么只读取第一个元素的数据的原因。在拨打//

的电话中摆脱SelectSingleNode
            currentBox.Compliment = currentBoxNode.SelectSingleNode("compliment").InnerText;
            currentBox.Description = currentBoxNode.SelectSingleNode("description").InnerText;
            currentBox.BoxType = currentBoxNode.SelectSingleNode("boxtype").InnerText;
            currentBox.InsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("il").InnerText);
            currentBox.InsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ib").InnerText);
            currentBox.InsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("ih").InnerText);
            currentBox.OutsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("ol").InnerText);
            currentBox.OutsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ob").InnerText);
            currentBox.OutsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("oh").InnerText);
            currentBox.BoxWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bw").InnerText);
            currentBox.BoxGrossWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bgw").InnerText);
            currentBox.BoxStrength = int.Parse(currentBoxNode.SelectSingleNode("boxstrength").InnerText);

您还可以使用./来指示当前上下文:

currentBox.Compliment = currentBoxNode.SelectSingleNode("./compliment").InnerText;
...