C# 在XSD生成的对象中设置文本内容
我有一个.dtd文件,它已转换为.xsd文件。此文件具有元素标识:C# 在XSD生成的对象中设置文本内容,c#,xml,xsd,dtd,C#,Xml,Xsd,Dtd,我有一个.dtd文件,它已转换为.xsd文件。此文件具有元素标识: <xs:element name="Identity"> <xs:complexType mixed="true"> <xs:sequence> <xs:any minOccurs="0" maxOccurs="unbounded" namespace="##any" /> </xs:sequence> &
<xs:element name="Identity">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##any" />
</xs:sequence>
<xs:attribute name="lastChangedTimestamp" type="xs:string" />
</xs:complexType>
</xs:element>
这将生成以下示例xml,其中包含一些文本内容:
<Identity lastChangedTimestamp="lastChangedTimestamp1" xmlns="http://tempuri.org/cXML">text</Identity>
文本
我使用xsd.exe将.xsd转换为.cs文件:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/cXML")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/cXML", IsNullable = false)]
public partial class Identity
{
public string Text { get; set; }
private System.Xml.XmlNode[] anyField;
private string lastChangedTimestampField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[] Any
{
get
{
return this.anyField;
}
set
{
this.anyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lastChangedTimestamp
{
get
{
return this.lastChangedTimestampField;
}
set
{
this.lastChangedTimestampField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“4.6.1055.0”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://tempuri.org/cXML")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://tempuri.org/cXML“,IsNullable=false)]
公共部分类标识
{
公共字符串文本{get;set;}
private System.Xml.XmlNode[]anyField;
私有字符串lastChangedTimestampField;
///
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[]任何
{
得到
{
返回这个.anyField;
}
设置
{
this.anyField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串lastChangedTimestamp
{
得到
{
返回此.lastChangedTimestampField;
}
设置
{
this.lastChangedTimestampField=值;
}
}
}
但是,这不允许我设置标识节点的文本内容:
var credential = new Credential()
{
Identity = new Identity()
{
lastChangedTimestamp = "lastChangedTimestamp1",
//Text = "MyRef" <- would like to set it at this point
}
};
var-credential=new-credential()
{
标识=新标识()
{
lastChangedTimestamp=“lastChangedTimestamp1”,
//Text=“MyRef”最后,我更新了xsd标识节点以添加
:
不确定这是否是最好的方法,但我现在可以使用它。mixed=“true”
在XSD中表示
元素可以包含:
当元素类型的元素可能包含字符数据时,该元素类型的内容是混合的,可以选择在其中穿插元素
此外,子元素不受XSD的约束
xsd.exe
和XmlSerializer
通过和属性的组合支持混合内容。当这两个属性应用于XmlNode[]
类型的成员时,XML中包含的所有混合内容都将绑定到该成员
在本例中,您将注意到xsd.exe
已经创建了一个public System.Xml.XmlNode[]具有这些属性的任何成员,因此,要将文本“MyRef”添加到Xml中,您需要为此数组添加一个适当的:
var identity = new Identity()
{
lastChangedTimestamp = "lastChangedTimestamp1",
Any = new XmlNode []
{
new XmlDocument().CreateTextNode("MyRef"),
},
};
演示小提琴
或者,您可以手动将Any
属性更改为对象数组,如至所示:
然后您可以将字符串直接添加到数组中:
var identity = new Identity()
{
lastChangedTimestamp = "lastChangedTimestamp1",
Any = new object []
{
"MyRef",
},
};
(您仍然需要为更复杂的XML分配XmlNode
对象。)
Demo fiddle#2.我可以假设您手动添加了公共字符串文本{get;set;}
“文本”是我假设可以设置的属性,但在自动生成的代码中不存在。我希望避免更改系统生成的代码,这非常好,谢谢。我没有太多使用XML,因此创建XML文档来存储字符串是非常违反直觉的,但这是实现这一点的方法
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/cXML")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/cXML", IsNullable = false)]
public partial class Identity
{
private string lastChangedTimestampField;
private string[] textField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lastChangedTimestamp
{
get
{
return this.lastChangedTimestampField;
}
set
{
this.lastChangedTimestampField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
}
new Credential()
{
Identity = new Identity()
{
Text=new string[]
{
"MyRef"
}
}
}
var identity = new Identity()
{
lastChangedTimestamp = "lastChangedTimestamp1",
Any = new XmlNode []
{
new XmlDocument().CreateTextNode("MyRef"),
},
};
[XmlText(typeof(string))]
[XmlAnyElement]
public object[] Any { get; set; }
var identity = new Identity()
{
lastChangedTimestamp = "lastChangedTimestamp1",
Any = new object []
{
"MyRef",
},
};