这是我的xml的片段,
<e80:td941Grp>
<e80:td941KeyGrp>
<e80:membExchIdCod>XX445678</e80:membExchIdCod>
<e80:membExchIdNam>XX44567899123</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>
这是我的代码,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace WindowsFormsApplication1
{
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; set; }
public string qouReqTot { get; set; }
public string dCutLim { get; set; }
public string goodQuoRepResp { get; set; }
public string quoReqViol { get; set; }
public string shtQuoPct { get; set; }
public string valQuoReqViol { get; set; }
public string ValQuoRepTot { get; set; }
public string valGoodQuoReqResp { get; set; }
public string violPct { get; set; }
}
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 = @"........xxxxx.xml";
XDocument xdoc1 = XDocument.Load(xmlDoc);
td941Grp objtd941 = new td941Grp();
List<td941Grp> listtd941 = (from _td941 in xdoc1.Element("e80:td941")
.Elements("e80:td941Grp")
select new td941Grp
{
td941 = _td941.Element("e80:td941Grp").Value,dataList = (from _record in _td941.
Element("e80:td941Grp").Elements("e80:td941Rec")
select new td941Rec
{
prodID = _td941.Element("prodID").Value,
qouReqTot = _td941.Element("qouReqTot").Value,
}).ToList()
}).ToList();
}
我收到错误XmlException未处理“''''字符,十六进制值0x3A,不能包含在名称中。”
我对c#相对较新,但在xml中总是新手所以任何帮助都会非常感激。
谢谢,
答案 0 :(得分:2)
您无法使用.Element("e80:td941")
而是使用XNamespace:
XNamespace nsE80 = xmlDoc.GetNamespaceOfPrefix("e80");
... .Element(nsE80 + "td941")