C# 如何使用内部标记和内部元素反序列化XML文档?
这是我的XML文件C# 如何使用内部标记和内部元素反序列化XML文档?,c#,xml,object,deserialization,C#,Xml,Object,Deserialization,这是我的XML文件 <getMetadata> <Project Name="Doors_Demo"> <Module FullPath="/Doors_Demo/Test_Module2"> <Attributes> <Attribute name="TableType" type="TableType" /> <Attribute name="TableTopBorder" type="Table
<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反序列化到列表中,但我无法访问根元素中的所有内部元素和属性
我希望将模块元素及其内部元素和标记的详细信息放入可以访问的列表中。下面是一个从XML自动生成类的小技巧 首先,创建一个新的空类,将其命名为例如
TempXml
将XML复制到剪贴板并打开刚创建的新空类
转到Visual Studio“编辑”菜单,然后粘贴特殊内容并将XML粘贴为类:
这将生成以下代码:
/// <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;
}
}
}
//
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类getMetadata
{
私有getMetadataProject项目字段;
///
公共getMetadataProject项目
{
得到
{
返回此.projectField;
}
设置
{
this.projectField=值;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类getMetadataProject
{
私有getMetadataProjectModule模块字段;
私有字符串名称字段;
///
公共getMetadataProjectModule模块
{
得到
{
返回此.moduleField;
}
设置
{
this.moduleField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串名
{
得到
{
返回此.nameField;
}
设置
{
this.nameField=值;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类getMetadataProjectModule
{
私有getMetadataProjectModuleAttribute[]属性字段;
私有字符串fullPathField;
///
[System.Xml.Serialization.XmlArrayItemAttribute(“属性”,IsNullable=false)]
公共getMetadataProjectModuleAttribute[]属性
{
得到
{
返回此.attributesField;
}
设置
{
this.attributesField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串完整路径
{
得到
{
返回this.fullPathField;
}
设置
{
this.fullPathField=值;
}
}
}
///
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类getMetadataProjectModuleAttribute
{
私有字符串名称字段;
私有字符串类型字段;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串名
{
得到
{
返回此.nameField;
}
设置
{
this.nameField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串类型
{
得到
{
返回此.typeField;
}
设置
{
this.typeField=值;
}
}
}
对于XmlSerializer
类,它应该可以正常工作
您可以通过删除空注释、更改类的名称以使用camel case(在这种情况下,您需要像您在问题中所做的那样在属性中指定真实的元素名称)或将类移动到不同的文件来稍微清理生成的输出
希望有帮助。1 puke emoji用于包含名为“Attribute”的元素的文档……我只能获取项目名称,我想从xml中获取模块名称和属性,我该怎么做?
/// <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;
}
}
}