C#反序列化的XML对象 [英] C# Deserialize XML to object

查看:182
本文介绍了C#反序列化的XML对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些反序列化到XML在C#中的对象的问题。



这是我收到的是...



错误

 的xmlns =''>是没有预料到。 

这是我收到的产生我的课是如下...

$ B的XSD
$ b

 <?XML版本=1.0编码=UTF-8>?; 
< XS:架构的targetNamespace =xml.AAAAAAA.com/commerce/apres-vente_technique/assistance的xmlns:PGP =xml.AAAAAAA.com/commerce/apres-vente_technique/assistance的xmlns:XS = http://www.w3.org/2001/XMLSchema将elementFormDefault =合格attributeFormDefault =不合格>
< XS:元素的名称=ListeAvisRemboursements>
< XS:注释>
< XS:文件>清单当然德安飞士德remboursements< / XS:文件>
< / XS:注释>
< XS:复杂类型>
< XS:序列的maxOccurs =无界>
< XS:元素的名称=AvisRemboursementTYPE =PGP:AvisRemboursementType/>
< / XS:序列>
< / XS:复杂类型>
< / XS:组件>
< XS:复杂类型名称=AvisRemboursementType>
< XS:注释>
< XS:文件>安飞士德remboursement谎言UNE DC< / XS:文件>
< / XS:注释>
< XS:序列>



(剪断)



该文件我试图导入如下:?

 < XML版本=1.0编码=UTF-8? > 
< ListeAvisRemboursements的xmlns:AST =xml.AAAAAAA.com/commerce/apres-vente_technique/assistance>
< AST:AvisRemboursement NumeroDT =3826961CodeRA =020545G01NumeroDC =1>
< AST:DateTraitement>&2010-06-22 LT; / AST:DateTraitement>
< AST:MontantDC> 25.0 LT; / AST:MontantDC>
< AST:MontantMO> 0.0< / AST:MontantMO>
< AST:SommeAD> 25.0 LT; / AST:SommeAD>
< AST:MontantPR> 0.0< / AST:MontantPR>
< AST:SommePR> 0.0< / AST:SommePR>
< AST:FraisGestion> 0.0< / AST:FraisGestion>
< AST:NombreHeuresTotalRemboursees> 0℃; / AST:NombreHeuresTotalRemboursees>
< AST:行政法院> C< / AST:行政法院>
< AST:NoteCredit> 319984< / AST:NoteCredit>
< AST:归责> 030< / AST:归责>
< AST:ListInterventionsPR />
< AST:ListInterventionsMO />
< / AST:AvisRemboursement>



(剪断)



我的认为的正在发生的事情是,当净试图derserialize的XML,它击中包含的第一行的xmlns:AST而关于它的投诉。据我了解,净会尽量属性映射到目标类中的公共属性(它不会找到一个叫的xmlns,或者有什么问题,我如何处理的命名空间。



我的反序列化的代码如下:

  XmlDocument的_Doc =新的XmlDocument(); 
_Doc.Load(@C:\inputfile.xml);

XmlSerializer的_XMLSer =新的XmlSerializer(typeof运算(ListeAvisRemboursements));
ListeAvisRemboursements _X =(ListeAvisRemboursements)_XMLSer.Deserialize (新StringReader(_Doc.OuterXml));

我也尝试添加一个命名空间管理的各种组合XML文档。

 的XmlNamespaceManager _Ns =新的XmlNamespaceManager(_Doc.NameTable); 
_Ns.AddNamespace( AST,xml.AAAAAAA.com/commerce/apres-vente_technique/assistance);

我明白有一种方法,我可以用这个来告诉.NET来接受什么样的命名空间。



将不胜感激一些帮助iwth这个问题。



---在更新带班片断请求(抱歉,应该以前也包括在内)这是与XSD.EXE ---



<$ p $创建p> ///<言论/>
[System.CodeDom.Compiler.GeneratedCodeAttribute(XSD,4.0.30319.1)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(准则)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = TRUE,命名空间=xml.AAAAAAA.com/commerce/apres-vente_technique/assistance)]
[System.Xml.Serialization.XmlRootAttribute(命名空间=xml.AAAAAAA.com/commerce/apres-vente_technique/assistance,ISNULLABLE = FALSE)]
公共部分类ListeAvisRemboursements
$ { b
$ b私人AvisRemboursementType [] avisRemboursementField;

///<言论/>
[System.Xml.Serialization.XmlElementAttribute(AvisRemboursement)]
公共AvisRemboursementType [] AvisRemboursement
{
得到
{
返回this.avisRemboursementField ;
}

{
this.avisRemboursementField =价值;
}
}
}

///<言论/>
[System.CodeDom.Compiler.GeneratedCodeAttribute(XSD,4.0.30319.1)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(准则)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=xml.AAAAAAA.com/commerce/apres-vente_technique/assistance)]
公共部分类AvisRemboursementType
{

私人的System.DateTime dateTraitementField;

私人双montantDCField;

私人双montantMOField;

私人双sommeADField;

私人双montantPRField;


解决方案

如果没有一个完整的XSD / XML,或(或者)你的C#类,我们无法重现。但是,从XML向上的工作,这工作对我很好;意思是:误差不(据我可以看到)中的代码/数据您发布。你可以发布更完整的(可重复)的例子?



 公共类ListeAvisRemboursements 
{
私人只读名单,LT ; AvisRemboursement>项目=新的List< AvisRemboursement>();
[的XmlElement(AvisRemboursement,命名空间=xml.AAAAAAA.com/commerce/apres-vente_technique/assistance)]
公开名单< AvisRemboursement>项目{{返回的物品; }}
}
公共类AvisRemboursement
{
[XmlAttribute]公共字符串NumeroDT {获取;设置;}
[XmlAttribute]公共字符串CodeRA {获取;设置; }
[XmlAttribute]公共字符串NumeroDC {获取;设置;}
公众的DateTime DateTraitement {搞定;组; }
公共小数MontantDC {搞定;组; }
公共小数MontantMO {搞定;组; }
公共小数SommeAD {搞定;组; }
公共小数MontantPR {搞定;组; }
公共小数SommePR {搞定;组; }
公共小数FraisGestion {搞定;组; }
公众诠释NombreHeuresTotalRemboursees {搞定;组; }
公共字符串行政法院{搞定;组; }
公共字符串NoteCredit {搞定;组; }
公共字符串归责{搞定;组; }
}
静态无效的主要()
{
变种SER =新的XmlSerializer(typeof运算(ListeAvisRemboursements));
变种包装=(ListeAvisRemboursements)ser.Deserialize(新StringReader(XML));
//检查wrapper.Items等
}



也工作正常:

  VAR SER =新的XmlSerializer(typeof运算(ListeAvisRemboursements));使用(VAR读卡器= XmlReader.Create(inputfile.xml))
{
VAR包装=(ListeAvisRemboursements)ser.Deserialize(阅读器)
;
}

  XmlDocument的_Doc =新的XmlDocument(); 
_Doc.Load(inputfile.xml);
变种SER =新的XmlSerializer(typeof运算(ListeAvisRemboursements));
VAR包装=(ListeAvisRemboursements)ser.Deserialize(新StringReader(_Doc.OuterXml));

  XmlDocument的_Doc =新的XmlDocument(); 
_Doc.Load(inputfile.xml);
变种SER =新的XmlSerializer(typeof运算(ListeAvisRemboursements));
VAR包装=(ListeAvisRemboursements)ser.Deserialize(新XmlNodeReader对象(_Doc.DocumentElement));


Having problems deserializing some xml into an object in C#.

The error that I receive is...

xmlns=''> was not expected.

The XSD that I received to generate my class is as follows...

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance" xmlns:pgp="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="ListeAvisRemboursements">
        <xs:annotation>
            <xs:documentation>Liste des avis de remboursements</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="AvisRemboursement" type="pgp:AvisRemboursementType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="AvisRemboursementType">
        <xs:annotation>
            <xs:documentation>Avis de remboursement lié à une DC</xs:documentation>
        </xs:annotation>
        <xs:sequence>

(snipped)

The file that I am attempting to import is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<ListeAvisRemboursements xmlns:ast="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance">
    <ast:AvisRemboursement NumeroDT="3826961" CodeRA="020545G01" NumeroDC="1">
        <ast:DateTraitement>2010-06-22</ast:DateTraitement>
        <ast:MontantDC>25.0</ast:MontantDC>
        <ast:MontantMO>0.0</ast:MontantMO>
        <ast:SommeAD>25.0</ast:SommeAD>
        <ast:MontantPR>0.0</ast:MontantPR>
        <ast:SommePR>0.0</ast:SommePR>
        <ast:FraisGestion>0.0</ast:FraisGestion>
        <ast:NombreHeuresTotalRemboursees>0</ast:NombreHeuresTotalRemboursees>
        <ast:Etat>C</ast:Etat>
        <ast:NoteCredit>319984</ast:NoteCredit>
        <ast:Imputation>030</ast:Imputation>
        <ast:ListInterventionsPR/>
        <ast:ListInterventionsMO/>
    </ast:AvisRemboursement>

(snipped)

I think what is happening is that when .Net attempts to derserialize the xml, it hits the first line which contains the "xmlns:ast" and complaints about it. As I understand it, .Net will try to map attributes to a public property in the target class (and it wont find one called xmlns. Or there is something wrong with how I am handling the name spaces.

My deserialization code looks as follows:

    XmlDocument _Doc = new XmlDocument();
    _Doc.Load(@"C:\inputfile.xml");

    XmlSerializer _XMLSer = new XmlSerializer(typeof(ListeAvisRemboursements));
    ListeAvisRemboursements _X = (ListeAvisRemboursements)_XMLSer.Deserialize(new StringReader(_Doc.OuterXml));

I have also tried various combinations of adding a namespace manager to the XML document..

XmlNamespaceManager _Ns = new XmlNamespaceManager(_Doc.NameTable);
_Ns.AddNamespace("ast", "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance");

I understand there is a way I can use this to tell .Net what namespaces to accept.

Would be greatful for some help iwth this problem.

--- Updated at request with class snippet (sorry should have included before) this was created with xsd.exe ---

 /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance", IsNullable = false)]
    public partial class ListeAvisRemboursements
    {

        private AvisRemboursementType[] avisRemboursementField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("AvisRemboursement")]
        public AvisRemboursementType[] AvisRemboursement
        {
            get
            {
                return this.avisRemboursementField;
            }
            set
            {
                this.avisRemboursementField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
    public partial class AvisRemboursementType
    {

        private System.DateTime dateTraitementField;

        private double montantDCField;

        private double montantMOField;

        private double sommeADField;

        private double montantPRField;

解决方案

Without a full xsd / xml, or (alternatively) your C# classes, we can't reproduce. But working from the xml upwards, this works fine for me; meaning: the error is not (as far as I can see) in the code/data you posted. Can you post a more complete (reproducible) example?

public class ListeAvisRemboursements
{
    private readonly List<AvisRemboursement> items = new List<AvisRemboursement>();
    [XmlElement("AvisRemboursement", Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
    public List<AvisRemboursement> Items { get { return items; } }
}
public class AvisRemboursement
{
    [XmlAttribute] public string NumeroDT {get;set;}
    [XmlAttribute] public string CodeRA {get;set;}
    [XmlAttribute] public string NumeroDC {get;set;}
    public DateTime DateTraitement { get; set; }
    public decimal MontantDC { get; set; }
    public decimal MontantMO { get; set; }
    public decimal SommeAD { get; set; }
    public decimal MontantPR { get; set; }
    public decimal SommePR { get; set; }
    public decimal FraisGestion { get; set; }
    public int NombreHeuresTotalRemboursees { get; set; }
    public string Etat { get; set; }
    public string NoteCredit { get; set; }
    public string Imputation { get; set; }
}
static void Main()
{
    var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
    var wrapper = (ListeAvisRemboursements)ser.Deserialize(new StringReader(xml));
    // inspect wrapper.Items etc
}

also works fine with:

var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
using (var reader = XmlReader.Create("inputfile.xml"))
{
    var wrapper = (ListeAvisRemboursements)ser.Deserialize(reader);
}

and:

XmlDocument _Doc = new XmlDocument();
_Doc.Load("inputfile.xml");
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
var wrapper = (ListeAvisRemboursements)ser.Deserialize(new StringReader(_Doc.OuterXml));

and

XmlDocument _Doc = new XmlDocument();
_Doc.Load("inputfile.xml");
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
var wrapper = (ListeAvisRemboursements)ser.Deserialize(new XmlNodeReader(_Doc.DocumentElement));

这篇关于C#反序列化的XML对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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