无法解析在ASP.Net与放大器使用LINQ XML; C# [英] Unable to Parse XML using LINQ in ASP.Net & C#

查看:121
本文介绍了无法解析在ASP.Net与放大器使用LINQ XML; C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<Horizon-Export>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:41:08 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>
  </UltimateForce>
  <UltimateStress>
  </UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:47:10 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>25.3</UltimateForce>
  <UltimateStress>1.12</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:48:57 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>8.3</UltimateForce>
  <UltimateStress>0.37</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:49:20 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>10.9</UltimateForce>
  <UltimateStress>0.49</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:49:42 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>2.6</UltimateForce>
  <UltimateStress>0.12</UltimateStress>
  <Remarks>Pass</Remarks>
</Horizon-Export>

我的CS:

private List<CubeTestDTL> ParseXMLToList()
    {
        List<CubeTestDTL> cubeTestDetailList = new List<CubeTestDTL>();
        if (fuImport.HasFile)
        {
            if (fuImport.PostedFile.ContentLength > 0)
            {
                var name = string.Format("{0}.xml", Guid.NewGuid().ToString().Replace("-", string.Empty));
                var filePath  = string.Format("~/Temp/{0}",name);
                fuImport.SaveAs(Server.MapPath(filePath));
                cubeTestDetailList =
                (
                    from e in XDocument.Load(Server.MapPath(string.Format("~/Temp/{0}",name))).Root.Elements("Horizon-Export")
                    select new CubeTestDTL
                    {
                        Srlno = (int)e.Element("BatchNo."),
                        Accd = Session["ACCD"].ToString(),
                        AvgCompStrength = 0,
                        BreakingLoad = (double)e.Element("UltimateForce"),
                        CompStrength = (double)e.Element("UltimateStress"),
                        CubeArea = (double)e.Element("Area"),
                        CubeDensity = (double)e.Element("Density"),
                        CubeNo = (string)e.Element("SpecimenID"),
                        CubeWeight = (double)e.Element("Weight"),
                        CustDate = (DateTime)e.Element("DateofCasting"),
                        Grade = (string)e.Element("Grade"),
                        Location = (string)e.Element("OperatorName"),
                        MainAccd =Session["MAINACCD"].ToString(),
                        TargetStrength = 0,
                        TestOn = (double)e.Element("TestDate"),

                    }).ToList<CubeTestDTL>();


            }
        }
        return cubeTestDetailList;

    }

问题:

每一次解析返回我空的。我无法调试作为整个LINQ块经过一次。伯爵表始终显示为0。

Every time the parsing returns me 0 items. I am unable to debug as the whole linq block passes at once. List Count is always showing =0.

推荐答案

要没有得到除权pression任何结果这是正常的:

It's normal to not get any results from the expression:

from e in (LoadXmlStuff()).Root.Elements("Horizon-Export")

因为在XML的情况下&LT;地平线出口方式&gt; 的根,没有根的后代。

because in the case of your XML <Horizon-Export> is the root, not a descendant of the root.

这也意味着,不能有多个&LT;地平线出口&gt;在该XML 标签,因为一个XML只能有一个根。有多个标签的唯一可能性是将它们包装在另一个标签,那么这将成为根。

This also means that there cannot be multiple <Horizon-Export> tags in the XML, since an XML can only have one root. The only possibility to have more tags would be to wrap them in another tag, which would then become the root.

这篇关于无法解析在ASP.Net与放大器使用LINQ XML; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆