-1

这是我的 XML 的片段

<?xml version="1.0" encoding="ISO-8859-1"?>
-<e80:td941 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="https://www.xxx.html e80_td941.xsd 
https://www.xxx.html e80_reports_common_structs.xsd 
https://www.xxx.html e80_reports_data_types.xsd" 
xmlns:e80="https://www.xxx.html">
-<e80:rptHd>
<e80:exchNam>X</e80:exchNam>
<e80:envText>P</e80:envText>
<e80:rptCod>td941</e80:rptCod>
<e80:rptNam>Daily performance</e80:rptNam>
<e80:rptPrntEffDat>2013-08-09</e80:rptPrntEffDat>
<e80:rptPrntRunDat>2013-08-10</e80:rptPrntRunDat>
</e80:rptHd>
-<e80:td941Grp>
  -<e80:td941KeyGrp>
     <e80:membExchIdCod>xx999</e80:membExchIdCod>
     <e80:membExchIdNam>xx997ed</e80:membExchIdNam>
  </e80:td941KeyGrp>
 -<e80:td941Rec>
    <e80:prodId>5AB</e80:prodId>
    <e80:quoReqTot>0</e80:quoReqTot>
    <e80:dCutLim>150</e80:dCutLim>
    <e80:goodQuoReqResp>0</e80:goodQuoReqResp>
    <e80:quoReqViol>0</e80:quoReqViol>
    <e80:shtQuoPct>0.00</e80:shtQuoPct>
    <e80:valQuoReqViol>0</e80:valQuoReqViol>
    <e80:valQuoReqTot>0</e80:valQuoReqTot>
    <e80:valGoodQuoReqResp>0</e80:valGoodQuoReqResp>
    <e80:violPct>0.00</e80:violPct>
</e80:td941Rec>
.
.
.
</e80:td941Grp>
</e80:td941>

从这里可以看出,代码的目的是将这些元素放入一个类对象中,所以到目前为止我已经这样做了,

public class td941Rec
{


    private string _prodID;
    private string _qouReqTot;
    private string _dCutLim;
    private string _goodQuoRepResp;
    private string _quoReqViol;
    private string _shtQuoPct;
    private string _valQuoReqViol;
    private string _ValQuoRepTot;
    private string _valGoodQuoReqResp;
    private string _violPct;


    public string prodID { get { return _prodID; } set { _prodID = value; } }
    public string qouReqTot { get { return _qouReqTot; } set { _qouReqTot = value; } }
    public string dCutLim { get { return _dCutLim; } set { _dCutLim = value; } }
    public string goodQuoRepResp { get { return _goodQuoRepResp; } set {
                                                         _goodQuoRepResp = value; } }
    public string quoReqViol {get {return _quoReqViol; } set { _quoReqViol = value; } }
    public string shtQuoPct { get { return _shtQuoPct; } set { _shtQuoPct = value; } }
    public string valQuoReqViol {get{return _valQuoReqViol; } set {_valQuoReqViol 
                                                                    = value;}}
    public string ValQuoRepTot { get { return _ValQuoRepTot; } set { _ValQuoRepTot 
                                                                         = value; } }
    public string valGoodQuoReqResp { get { return _valGoodQuoReqResp; } set { 
                                                     _valGoodQuoReqResp = value; } }
    public string violPct { get { return _violPct; } set { _violPct = value; } }

}

public class td941Grp
{
    private string _td941;
    public string td941 { get { return _td941; } set { _td941 = value; } }

    public List<td941Rec> dataList = new List<td941Rec>();
}


class QoutePerformance
{


    public void xmltoExcel()
    {
        string xmlDoc = @"C:......xml";

        XDocument xdoc1 = XDocument.Load(xmlDoc);
        XNamespace ns = "e80";
        td941Grp objtd941 = new td941Grp();

        List<td941Grp> listtd941 = (from _td941 in xdoc1.Element(ns +
                                               "td941").Elements(ns + "td941Grp")
                          select new td941Grp
                          {
                          td941 = _td941.Element(ns + "td941Grp").Value,
                          dataList = (from _record in _td941.Element(ns +
                                       "td941Grp").Elements(ns + "td941Rec")
                          select new td941Rec
                          {
                           prodID = _td941.Element(ns + "prodID").Value,
                           qouReqTot = _td941.Element(ns + "qouReqTot").Value,
                           dCutLim = _td941.Element(ns + "dCutlim").Value,
                           goodQuoRepResp = _td941.Element(ns + "goodQuoRepResp").Value


                                                 }).ToList()


                                 }).ToList();



    }

(请原谅代码的格式)

我已经阅读了很多页面并试图理解它。但似乎无法让它发挥作用。上面的代码返回一个未设置为对象实例的 nullException 对象引用(但我有一种感觉,即使解决了,它也不起作用。有人能告诉我如何将 xml 放入一个列表?

首先十分感谢

4

1 回答 1

2

这是问题(或至少问题):

XNamespace ns = "e80";

您正在指定alias,这并不是真正的命名空间。你要:

XNamespace ns = "https://www.xxx.html";

如此处在 XML 中指定的:

xmlns:e80="https://www.xxx.html"

因为您指定了错误的命名空间,所以您的Element调用正在返回null,然后您将取消引用。

于 2013-08-14T15:24:12.660 回答