Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取所有同级节点元素_C#_Xml - Fatal编程技术网

C# 读取所有同级节点元素

C# 读取所有同级节点元素,c#,xml,C#,Xml,我遇到的情况是,我有一个如下所示的xml文件: <initialize> <boxes> <box> <compliment>RayTest1</compliment> <description>RayTest1</description> <boxtype>RayTest</boxtype> <il>1.00</il>

我遇到的情况是,我有一个如下所示的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>

光线测试1
光线测试1
光线测试
1
1
1
1
1
1
1
1
1.
光线测试2
光线测试2
光线测试
99.99
99.99
99.99
99.99
99.99
99.99
99.99
99.99
99
我在C#中需要完成的是:

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

我需要解析xml文件的每个框,将值分配给一个对象,然后将该对象添加到一个列表中,以便在ASP.NET MVC应用程序中显示

我已尝试通过以下方式完成此操作:

                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";
                }
试试看
{
列表框列表=新列表();
XmlDocument xmlDoc=新的XmlDocument();
加载(fileFromPage.FileName);
XmlNodeList-boxNodeList=xmlDoc.SelectSingleNode(“//初始化/box”).ChildNodes;
foreach(boxNodeList中的XmlNode boxNode)
{
XmlNode currentBoxNode=boxNode;
BoxViewModel currentBox=新的BoxViewModel();
currentBox.compaid=currentBoxNode.SelectSingleNode(“//compaid”).InnerText;
currentBox.Description=currentBoxNode.SelectSingleNode(//Description”).InnerText;
currentBox.BoxType=currentBoxNode.SelectSingleNode(//BoxType”).InnerText;
currentBox.InsidelLength=decimal.Parse(currentBoxNode.SelectSingleNode(“//il”).InnerText);
currentBox.InsideBradth=decimal.Parse(currentBoxNode.SelectSingleNode(“//ib”).InnerText);
currentBox.InsideHight=decimal.Parse(currentBoxNode.SelectSingleNode(“//ih”).InnerText);
currentBox.OutsideLength=decimal.Parse(currentBoxNode.SelectSingleNode(“//ol”).InnerText);
currentBox.OutsideWidth=decimal.Parse(currentBoxNode.SelectSingleNode(“//ob”).InnerText);
currentBox.OutsideHeight=decimal.Parse(currentBoxNode.SelectSingleNode(“//oh”).InnerText);
currentBox.BoxWeight=decimal.Parse(currentBoxNode.SelectSingleNode(“//bw”).InnerText);
currentBox.BoxGrossWight=decimal.Parse(currentBoxNode.SelectSingleNode(“//bgw”).InnerText);
currentBox.BoxStrength=int.Parse(currentBoxNode.SelectSingleNode(“//BoxStrength”).InnerText);
boxesList.Add(当前框);
}
}
抓住
{
ViewBag.MsgText=“处理文件时出错!”;
ViewBag.MsgColor=“红色”;
}
我在这里遇到的问题是,当我这样做时,每个盒子都是一样的。它只得到第一个盒子。foreach循环足够聪明,可以知道有两个框,但它无法获取第二个框并将其添加到我的列表中。相反,我有一个相同盒子的两个列表


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

您引用的是XML文档的根,这就是为什么只读取第一个元素中的数据。取消对
选择SingleNode
的调用中的
/

            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;
...

您正在引用XML文档的根,这就是为什么只读取第一个元素中的数据。取消对
选择SingleNode
的调用中的
/

            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;
...

您正在引用XML文档的根,这就是为什么只读取第一个元素中的数据。取消对
选择SingleNode
的调用中的
/

            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;
...

您正在引用XML文档的根,这就是为什么只读取第一个元素中的数据。取消对
选择SingleNode
的调用中的
/

            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;
...
而不是使用

XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;
尝试使用:

XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");
这将获得每个框节点的节点列表。

而不是使用

XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;
尝试使用:

XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");
这将获得每个框节点的节点列表。

而不是使用

XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;
尝试使用:

XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");
这将获得每个框节点的节点列表。

而不是使用

XmlNodeList boxNodeList = xmlDoc.SelectSingleNode("//initialize/boxes").ChildNodes;
尝试使用:

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

这应该会得到每个box节点的节点列表。

System.Xml
确实喜欢“吃”更多的东西。我建议改为使用LINQtoXML。所以,如果我有100或1000个框,我甚至会得到一个页面吗?或者你认为它会在MVC中超时吗?
System.Xml
真的喜欢“吃”比它应该吃的更多。我建议改为使用LINQtoXML。所以,如果我有100或1000个框,我甚至会得到一个页面吗?或者你认为它会在MVC中超时吗?
System.Xml
真的喜欢“吃”比它应该吃的更多。我建议改为使用LINQtoXML。所以,如果我有100或1000个框,我甚至会得到一个页面吗?或者你认为它会在MVC中超时吗?
System.Xml
真的喜欢“吃”比它应该吃的更多。我建议改为使用LINQtoXML。所以,如果我有100或1000个框,我甚至会得到一个页面吗?或者你认为MVC会暂停吗?是的!成功了!有没有一个网站我可以学习如何正确使用Xml相关类?我用它来学习XPath语法。非常好,有很多例子。对于
XmlDocument
有代码示例。您遇到的问题实际上是XPath语法。你可以看看是的!成功了!有没有一个网站我可以学习如何正确使用Xml相关类?我用它来学习XPath语法。非常好,有很多例子。对于
XmlDocumen