在代码中从 XML 创建 XSD

Create XSD from XML in Code(在代码中从 XML 创建 XSD)
本文介绍了在代码中从 XML 创建 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MSDN 中的这段代码从 XML 创建 XSD

I am using this piece of code from MSDN to create an XSD from an XML

XmlReader reader = XmlReader.Create("contosoBooks.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();

schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())
{
   textbox.text = s.ToString();
}

我想根据我的 xml 文件输出 .xsd.当我生成 .xsd 文件时,我得到的唯一内容是:System.Xml.Schema.XmlSchema

I want to output the .xsd based on my xml file. When I generate the .xsd file, the only content I get inside it is: System.Xml.Schema.XmlSchema

当我使用 Visual Studio 选项生成 XSD 来创建架构时,它会正确显示.但是,我有超过 150 个 xml 文档需要创建 XSD,因此需要一个编程选项.有人可以帮忙吗?

When I generate the XSD using Visual Studio option to create Schema, it comes out properly. However, I have over 150 xml docs that I need to create XSD for hence need a programmatic option. Can anyone help?

推荐答案

这就是你缺少的...而不是简单地执行 s.ToString(),而是这样做:

This is what you're missing... instead of simply doing s.ToString(), do this:

XmlWriter writer;
int count = 0;
foreach (XmlSchema s in schemaSet.Schemas())
{
    writer = XmlWriter.Create((count++).ToString() + "_contosobooks.xsd");
    s.Write(writer);
    writer.Close();
    Console.WriteLine("Done " + count);
}
reader.Close();

然后您可以编写适当的逻辑来更优雅地进行读/写,读取许多 xml 文件并创建相应的 xsd 文件等.

You can then write proper logic to do the read/write more gracefully, read many xml files and create corresponding xsd files, etc.

我从这里获取了 contosobooks.xml:https://code.google.com/p/code4cs/source/browse/trunk/AppCase/dNet/Xml/data/contosoBooks.xml?spec=svn135&r=135

I took the contosobooks.xml from here: https://code.google.com/p/code4cs/source/browse/trunk/AppCase/dNet/Xml/data/contosoBooks.xml?spec=svn135&r=135

输出 xsd 为:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这篇关于在代码中从 XML 创建 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)