向XmlDocument添加遵守XSD的新元素 [英] Adding new elements to XmlDocument that abide by XSD

查看:91
本文介绍了向XmlDocument添加遵守XSD的新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用XPath表示法向XmlDocument中添加元素,为此我编写了代码,将该元素放置在文件中的适当位置.除了一个例外.我不知道该如何注意XSD文件中定义的顺序规则.

有没有一种方法可以将元素添加到XmlDocument中,从而遵守管理我的XML文件的XSD中定义的顺序?

例如,我的xml文档应如下所示:

Currently, I''m adding elements to my XmlDocument using XPath notation for which I''ve written code to that places the element at the proper location in the file. With one exception. I don''t know how to make it pay attention to the sequence rules defined in my XSD file.

Is there a way to add an element to an XmlDocument so that is abides by the sequence define in the XSD that governs my XML file?

For example, my xml document should look like:

<rootTag>
  <area name="I define an area">
    <description>some text here</description>
    <point x="1" y="1" />
    <point x="2" y="2" />
    <point x="3" y="3" />
  </area>
</rootTag>


但是我得到了,这取决于用户为上面的子标记输入值的顺序:


Yet I get, depending on the order in which the user enters values for the child tags above:

<rootTag>
  <area name="I define an area">
    <point x="1" y="1" />
    <point x="2" y="2" />
    <point x="3" y="3" />
    <description>some text here</description>
  </area>
</rootTag>


为了纠正上述问题,我从XSD文件创建了一个数据集(名为tempXmlDataset).我将XmlDocument的内容传递到tempXmlDataset中,然后适当地重新排序.

但是,我的问题是由XML文档的第一个子项的选项引起的.此选项在XSD中定义为允许区域",线"或点"对象. 区域"和线"都具有点"元素作为子元素.但是子级点"与点"对象不同.因此,您可能已经意识到,tempXmlDataset.ReadXmlSchema(...)创建一个仅包含x和y的点"表.根据定义,这是区域"和线"的子级.

因此,当我的代码运行tempXmlDataset.ReadXml(...)时,不会读取点"对象的属性,因为它将点"对象视为子点".这是点"对象的示例:


To correct the above, I create a DataSet (named tempXmlDataset) from the XSD file. I pass the contents of the XmlDocument into tempXmlDataset and things get re-ordered appropriately.

However, my problem is caused by an option for the first child of the XML document. This option is defined in the XSD to allow for "area", "line" or "point" objects. "area" and "line" both have "point" elements as children. But child "point" is not the same as "point" object. So, as you might already realize, tempXmlDataset.ReadXmlSchema(...) creates a "point" table which only has x and y in it. This is by definition of the children for "area" and "line".

So when my code runs tempXmlDataset.ReadXml(...) the attributes for "point" object do not get read in because it sees "point" object as child "point". Here''s an example of "point" object:

<rootTag>
  <point name="I define a point" x="3" y="3" otherAttributes="">
    <description>some text here</description>
  </point>
</rootTag>

推荐答案

看看 .NET Framework中的极端XML:XML序列化 [
Have a look at xsd.exe[^]

The XML Schema Definition tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

Update
Have a look at Extreme XML:XML Serialization in the .NET Framework[^]

Best regards
Espen Harlinn


XMLDocument使用DOM,因此您可以使用各种方法在任何需要的地方插入节点.

http://msdn.microsoft.com/en-us/library/system. xml.xmlnode.prependchild.aspx [ ^ ]

可能就是您想要的.
XMLDocument uses the DOM, so you have methods to insert nodes where-ever you like.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.prependchild.aspx[^]

is probably what you want.


这篇关于向XmlDocument添加遵守XSD的新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆