我在dtd:h.html后面有一个xml文件
您可以注意到,两个元素的名称中包含带有冒号(:)的属性:
lsource和gloss可以包含一个名为xml:lang的属性,如本例所示(对于lsource元素):
<entry>
<ent_seq>1002480</ent_seq>
<k_ele>
<keb>お転婆</keb>
</k_ele>
<k_ele>
<keb>御転婆</keb>
</k_ele>
<r_ele>
<reb>おてんば</reb>
</r_ele>
<sense>
<pos>&adj-na;</pos>
<pos>&n;</pos>
<misc>&uk;</misc>
<lsource xml:lang="dut">ontembaar</lsource>
<gloss>tomboy</gloss>
</sense>
</entry>我不知道如何定义表示lsource元素的类,这里现在是它,但是它缺少这个属性:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2046.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "JMdict_e.dtd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "JMdict_e.dtd", IsNullable = false)]
public partial class lsource
{
private string ls_typeField;
private string ls_waseiField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ls_type
{
get
{
return this.ls_typeField;
}
set
{
this.ls_typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ls_wasei
{
get
{
return this.ls_waseiField;
}
set
{
this.ls_waseiField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}如何为XmlSerializer命名属性以正确识别和解析该属性?我尝试添加属性public string xml_lang { get; set; }或public string lang { get; set; },但当xml文件调用xml文件时,两者都无法解析该属性。
发布于 2017-12-18 08:40:19
该属性位于未生成的命名空间中,因此元素/属性将被愉快地忽略。用它所在的名称空间装饰lang属性将有效:
[System.Xml.Serialization.XmlAttributeAttribute(Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang {
get;set;
}xml名称空间是W3C定义的标准命名空间。它的值可以找到这里。
https://stackoverflow.com/questions/47864498
复制相似问题