XML 到列表代码中的错误。名称中不能包含':'字符(十六进制值0x3A)



这是我的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完全是个新手。因此,如果能提供任何帮助,我将不胜感激。

谢谢,

您不能使用.Element("e80:td941")

相反,使用XNamespace:

XNamespace nsE80 = xmlDoc.GetNamespaceOfPrefix("e80");
... .Element(nsE80 + "td941")

最新更新