如何使用内部标记和内部元素反序列化XML文档?

时间:2017-11-13 12:49:43

标签: c# xml object deserialization

这是我的XML文件。

Code = 24
    AddressLine = 000000000
    BusinessPhone = 040-44445511
    City = 0000000
    Country = NL 
    created = /Date(1493085780497)/
    Email = test@sportmartbv.nl
    BusinessFax = 000000
    FirstName = Ellis
    Your last name = Renners
    Title = MEVR
    Mobile = 0682121519
    Notes = 000000000
    Phone = 040-444151151
    State = 000000000
    Postcode = 00000000

我想反序列化上面的XML

以下是我的代码:

<getMetadata>
 <Project Name="Doors_Demo">
  <Module FullPath="/Doors_Demo/Test_Module2">
   <Attributes>
    <Attribute name="TableType" type="TableType" /> 
    <Attribute name="TableTopBorder" type="TableEdgeType" /> 
   </Attributes>
  </Module>
 </Project>
</getMetadata>

我想将我的XML反序列化为一个列表,我无法访问根元素中的所有内部元素和属性。

我想将模块元素及其内部元素和标签的详细信息放入可以访问的列表中。

1 个答案:

答案 0 :(得分:2)

这是一个自动从XML生成类的小技巧。

首先,创建一个新的空类,例如TempXml

将XML复制到剪贴板并打开刚刚创建的新空类。

转到Visual Studio 编辑菜单,然后选择性粘贴将XML粘贴为类

Paste XML as Classes

这将生成以下代码:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class getMetadata
{

    private getMetadataProject projectField;

    /// <remarks/>
    public getMetadataProject Project
    {
        get
        {
            return this.projectField;
        }
        set
        {
            this.projectField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProject
{

    private getMetadataProjectModule moduleField;

    private string nameField;

    /// <remarks/>
    public getMetadataProjectModule Module
    {
        get
        {
            return this.moduleField;
        }
        set
        {
            this.moduleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProjectModule
{

    private getMetadataProjectModuleAttribute[] attributesField;

    private string fullPathField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Attribute", IsNullable = false)]
    public getMetadataProjectModuleAttribute[] Attributes
    {
        get
        {
            return this.attributesField;
        }
        set
        {
            this.attributesField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FullPath
    {
        get
        {
            return this.fullPathField;
        }
        set
        {
            this.fullPathField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProjectModuleAttribute
{

    private string nameField;

    private string typeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}

哪个应该适用于XmlSerializer类。

您可以通过删除空注释来清理生成的输出,更改类的名称以使用驼峰大小写(在这种情况下,您需要在属性中指定实际的元素名称,就像您在问题)或将类移动到不同的文件。

希望它有所帮助。