来自带有内联 Xsd 的 XML 文件的 XmlSchema 对象

发布于 2024-09-29 18:39:38 字数 300 浏览 1 评论 0原文

在 .Net 中,我试图从带有嵌入式 Xsd 的 Xml 文件中获取 XmlSchema 对象,但不知道该怎么做?有人知道吗?

例如,如果它只是一个 Xml 文件,我可以使用 XmlSchemaInference 类推断架构,或者如果它是一个 Xsd,我可以使用 XmlSchema 类,但找不到内联 Xsd。

示例文件位于 http://pastebin.com/7yAjz4Z4 (由于某种原因不会显示在此处)

谢谢你

In .Net, I'm trying to get a XmlSchema object from Xml file with an embedded Xsd and can not find out how to do it? anybody know?

For example if it just an Xml file I can Infer Schema using the XmlSchemaInference class or if its a Xsd I can use XmlSchema class, but can not find away with inlined Xsd.

example file is at http://pastebin.com/7yAjz4Z4 (for some reason wouldn't show on here)

Thank you

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

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

发布评论

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

评论(2

倒数 2024-10-06 18:39:38

这可以通过获取 xs:schema 元素节点的 XmlReader 并将其传递给 XmlSchema.Read 来完成。

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace EmbeddedXmlSchema
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace xs = "http://www.w3.org/2001/XMLSchema";
            XDocument doc = XDocument.Load("XMLFile1.xml");
            XmlSchema sch;
            using (XmlReader reader = doc.Element("ReportParameters").Element(xs + "schema").CreateReader())
            {
                sch = XmlSchema.Read(reader, null);
            }
        }
    }
}

(如果您使用的是 XmlDocument 而不是 XDocument,请查看 XmlNode.CreateNavigator().ReadSubtree()。)

This can be done by obtaining an XmlReader for the xs:schema element node and passing it to XmlSchema.Read.

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace EmbeddedXmlSchema
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace xs = "http://www.w3.org/2001/XMLSchema";
            XDocument doc = XDocument.Load("XMLFile1.xml");
            XmlSchema sch;
            using (XmlReader reader = doc.Element("ReportParameters").Element(xs + "schema").CreateReader())
            {
                sch = XmlSchema.Read(reader, null);
            }
        }
    }
}

(If you are using XmlDocument instead of XDocument, look into XmlNode.CreateNavigator().ReadSubtree().)

世界和平 2024-10-06 18:39:38

我最终选择了这个。非常感谢您的帮助。

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(file);

            XmlNodeList nodes =
                xmlDocument.GetElementsByTagName("schema", "http://www.w3.org/2001/XMLSchema");

            if (null != nodes && 0 != nodes.Count)
            {
                XmlReader reader = new XmlNodeReader(nodes[0]);
                XmlSchema schema = XmlSchema.Read(reader, null);

                // do stuff with schema 
            }
            else
            {
                throw new InvalidOperationException("No inline schema found.");
            }

I went for this in the end. Thank you very must for your help.

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(file);

            XmlNodeList nodes =
                xmlDocument.GetElementsByTagName("schema", "http://www.w3.org/2001/XMLSchema");

            if (null != nodes && 0 != nodes.Count)
            {
                XmlReader reader = new XmlNodeReader(nodes[0]);
                XmlSchema schema = XmlSchema.Read(reader, null);

                // do stuff with schema 
            }
            else
            {
                throw new InvalidOperationException("No inline schema found.");
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文