这是我的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引入列表?
非常感谢
答案 0 :(得分:2)
这是问题(或至少是 问题):
XNamespace ns = "e80";
您正在指定别名,它实际上不是命名空间。你想要:
XNamespace ns = "https://www.xxx.html";
如XML中所述:
xmlns:e80="https://www.xxx.html"
由于您指定了错误的命名空间,因此Element
调用正在返回null
,然后您将其解除引用。