以编程方式(C#)创建xml模式

问题描述:

假设我有以下xml模式,

Suppose, I have the following xml schema,

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Tab1">
<xs:complextype>
<xs:sequence>
<xs:element minoccurs="0" maxoccurs="10" name="Test1" type="xs:string" />
<xs:element minoccurs="0" maxoccurs="10" name="Test2" type="xs:string" />
<xs:element minoccurs="0" maxoccurs="10" name="Test3" type="xs:int" />
</xs:sequence>
</xs:complextype>
</xs:element>
</xs:schema>



然后,我必须在每个元素中添加一些其他值
(例如:state ="kerala" url ="mylinkurl")

因此,最终的xml模式输出将如下所示:
(请参阅元素标签)



then, I have to add some additional values with each element
(Eg: state="kerala" url="mylinkurl")

So the final xml schema output will be as follows:
(see the element tags)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Tab1">
<xs:complextype>
<xs:sequence>
<xs:element minoccurs="0" maxoccurs="10" name="Test1" type="xs:string" state="kerala" url="mylinkurl" />
<xs:element minoccurs="0" maxoccurs="10" name="Test2" type="xs:string" state="tamilnadu" url="mylinkurl" />
<xs:element minoccurs="0" maxoccurs="10" name="Test3" type="xs:int" />
</xs:sequence>
</xs:complextype>
</xs:element>
</xs:schema>



如何添加这些名称&以编程方式(使用C#)实现价值?请提供示例代码.



How can add these name & value programmatically (using c#)? Please provide a sample code.

看看 ^ ]类.

使用它,您可以将新的XElement和XAttribute及其值添加到现有XElement.
Take a look at the XElement[^] class.

With it you can add new XElements and XAttributes along with their values to an existing XElement.