向 XmlDocument 添加遵守 XSD 的新元素

发布于 01-04 13:18 字数 1362 浏览 5 评论 0原文

目前,我正在使用 XPath 表示法向 XmlDocument 添加元素,我已为其编写了代码,将元素放置在文件中的正确位置。但有一个例外。我不知道如何让它关注我的 XSD 文件中定义的序列规则。

有没有办法将元素添加到 XmlDocument,以便遵守管理我的 XML 文件的 XSD 中定义的序列?

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

<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>

然而,根据用户为上面的子标记输入值的顺序,我得到:

<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 中定义为允许“区域”、“线”或“点”对象。 “area”和“line”都有“point”元素作为子元素。但子“点”与“点”对象不同。因此,您可能已经意识到,tempXmlDataset.ReadXmlSchema(...) 创建一个“点”表,其中仅包含 x 和 y。这是根据“区域”和“线”子项的定义。

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

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

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云2025-01-11 13:19:00

使用 xsd.exe 根据类的 xsd 生成所需的代码。不要尝试为这种情况创建数据集。然后,您可以将生成的代码与 XmlSerializer 一起使用来生成所需的 xml 文件。

http://msdn.microsoft.com/en-us /library/system.xml.serialization.xmlserializer.aspx

另请参阅:
http://msdn.microsoft.com/en-us/library/ms950721.aspx

use xsd.exe to generate the required code based on the xsd for classes. Don't try to create the dataset for this case. You can then use the generated code together with the XmlSerializer to produce the needed xml files.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Also see:
http://msdn.microsoft.com/en-us/library/ms950721.aspx

征棹2025-01-11 13:18:59

由于您标记了此 C#,我假设您使用的是 .NET 平台。 System.Xml.Schema 将是最好的朋友。对于使用上述 API 生成 XML 的程序,该程序还附带您可以用来了解如何解决问题的源代码,我将使用 XmlSampleGenerator

生成示例 XML 所需要的正是您在限制用户在给定时间点可能输入的 XPath 方面所需要的。我相信您必须从一开始就根据您在编辑过程中的位置来限制您允许的 XPath,否则,一个错误就可能使整个方法变得毫无用处。

如果您从一开始就不进行限制,则可能无法尝试基于 XSD 重新排序(请阅读 也在SO中)...

Since you tagged this C#, I assume you're on the .NET platform. The System.Xml.Schema would be your best friend. For a program that uses the above API to generate XML, that also comes with source code you could use to understand how to solve your issue, I would use the XmlSampleGenerator.

Generating a sample XML requires exactly what you need in terms of constraining the XPath the user may enter at a given point in time. I believe you will have to constrain the XPath you allow based on where you are in the editing process, right from the beginning, otherwise, one single mistake could make the whole approach useless.

If you don't constrain from the beginning, it might be impossible to try to re-order based on an XSD (please read this also in SO)...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文