C# 如何将xml读入类中?

C# 如何将xml读入类中?,c#,xml,serialization,C#,Xml,Serialization,我想将xml反序列化为c#。 但我遇到了错误,无法修复它 错误是: "There is an error in XML document (1, 2)." ... "<A xmlns=''> was not expected." 课程为: [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diag

我想将xml反序列化为c#。 但我遇到了错误,无法修复它

错误是:

 "There is an error in XML document (1, 2)." 
 ...
 "<A xmlns=''> was not expected."
课程为:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "A-1.0", Namespace = "")]
[System.Xml.Serialization.XmlRootAttribute("A", Namespace = "", IsNullable = false)]  
public  class A10
{

    private string versField;

    private string typeField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}
并由以下人员进行测试:

   string xml = "<A vers=\"1.0\" type=\"pd\">";

   A10 a = (A10) XmlDeserializeFromString(xml, typeof(A10));
为什么会这样?如何解决这个问题?

。您的根元素缺少结束标记

使您的文档如下所示:

<A vers="1.0" type="pd"></A>

。您的根元素缺少结束标记

使您的文档如下所示:

<A vers="1.0" type="pd"></A>


能否包含XML的一个片段?您是否尝试过在类上方仅使用[System.Serializable()]呢?是的,我尝试过。错误仍在显示。另外,除非您的性能受到限制,否则我建议使用
System.Xml.Linq
命名空间解析Xml。它附带了
XDocument
XElement
XAttribute
。在解析XML时,通常认为每种方法都更易于使用。能否包含XML片段?您是否尝试过在类上方仅使用[System.Serializable()]呢?是的,我尝试过。错误仍在显示。另外,除非您的性能受到限制,否则我建议使用
System.Xml.Linq
命名空间解析Xml。它附带了
XDocument
XElement
XAttribute
。在解析XML时,通常认为每种方法都更易于使用。
<A vers="1.0" type="pd"></A>