在代码中从XML创建XSD [英] Create XSD from XML in Code

查看:53
本文介绍了在代码中从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来创建架构时,它会正确显示.但是,我需要创建XSD的150多个xml文档,因此需要编程选项.有人可以帮忙吗?

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:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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