Xsd2Code 帮助 - 生成的代码似乎与架构不匹配

发布于 2024-12-10 21:19:57 字数 2807 浏览 0 评论 0原文

我可能做的事情完全错误,但我创建了一个简单的测试模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyRoot">
    <xs:annotation>
        <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:choice>
            <xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:choice>
                        <xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:choice>
                    <xs:attribute name="SomeAttribute" type="xs:string"/>
                    <xs:attribute name="SomethingElse" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

一个根,两个孩子(一个可选)。

我从 VS2010 运行 Xsd2Code,生成的代码创建了两个“根”类(MyRoot 和 MyChildOne),但没有创建预期的 MyChildTwo。我本来期望一个带有 MyRoot.MyChildOne 的模型...

这是生成的代码:

using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;


public partial class MyRoot
{

    private List<object> itemsField;

    public MyRoot()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

public partial class MyRootMyChildOne
{

    private List<object> itemsField;

    private string someAttributeField;

    private string somethingElseField;

    public MyRootMyChildOne()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }

    public string SomeAttribute
    {
        get
        {
            return this.someAttributeField;
        }
        set
        {
            this.someAttributeField = value;
        }
    }

    public string SomethingElse
    {
        get
        {
            return this.somethingElseField;
        }
        set
        {
            this.somethingElseField = value;
        }
    }
}

我不明白如何将其序列化为有效的(符合架构的)XML 文件...

感谢您对我进行了这

方面的教育

I might be doing something totally wrong, but i created a simple test schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyRoot">
    <xs:annotation>
        <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:choice>
            <xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:choice>
                        <xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:choice>
                    <xs:attribute name="SomeAttribute" type="xs:string"/>
                    <xs:attribute name="SomethingElse" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

One root, two children (one optional).

I ran the Xsd2Code from VS2010 and the generated code created two "root" classes (MyRoot and MyChildOne) without creating the expected MyChildTwo. I would have expected a model with MyRoot.MyChildOne...

Here's the generated code:

using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;


public partial class MyRoot
{

    private List<object> itemsField;

    public MyRoot()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

public partial class MyRootMyChildOne
{

    private List<object> itemsField;

    private string someAttributeField;

    private string somethingElseField;

    public MyRootMyChildOne()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }

    public string SomeAttribute
    {
        get
        {
            return this.someAttributeField;
        }
        set
        {
            this.someAttributeField = value;
        }
    }

    public string SomethingElse
    {
        get
        {
            return this.somethingElseField;
        }
        set
        {
            this.somethingElseField = value;
        }
    }
}

I don't understand how can I serialize this into a valid (schema compliant) XML file...

Thanks for educating me on this

Cos

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

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

发布评论

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

评论(1

风吹雪碎 2024-12-17 21:19:57

如果您使用 xsd.exe 生成此类,它会为您提供相同的结果:

public partial class MyRoot {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))]
    [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

除了使用已知类型声明之外:

[System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]

因此,如果您考虑一下,这是有道理的。因为您的子类型 2 是字符串,而字符串是 XSD 中的简单类型,所以您可以将 System.String 的实例添加到 Items 数组中,然后使用上面的代码将其序列化。每个字符串都将包装在 节点中。

更新

要完成此工作,您需要创建类型,然后使用 XmlSerializer:

var root = new MyRoot();
root.Items = new object[2];
root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" };
root.Items[1] = "hello";

var ser = new XmlSerializer(typeof(MyRoot));
var memoryStream = new MemoryStream();
var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
var streamReader = new StreamReader(memoryStream, Encoding.UTF8);
ser.Serialize(xmlTextWriter, root);
memoryStream.Position = 0;

string xml = streamReader.ReadToEnd();

这为我们提供了以下 XML:

<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <MyChildOne SomeAttribute="test" SomethingElse="test" />
    <MyChildTwo>hello</MyChildTwo>
</MyRoot>

If you use xsd.exe to generate this class, it gives you the same thing:

public partial class MyRoot {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))]
    [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

Except for the use of the known type declarations:

[System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]

So it makes sense if you think about it. Because your child type 2 is a string and string is a simple type in XSD you can add instances of System.String to your Items array and then serialize this out using the above code. Each string will be wrapped in a <MyChildTwo/> node.

UPDATE

To make this work you create your type and then use XmlSerializer:

var root = new MyRoot();
root.Items = new object[2];
root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" };
root.Items[1] = "hello";

var ser = new XmlSerializer(typeof(MyRoot));
var memoryStream = new MemoryStream();
var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
var streamReader = new StreamReader(memoryStream, Encoding.UTF8);
ser.Serialize(xmlTextWriter, root);
memoryStream.Position = 0;

string xml = streamReader.ReadToEnd();

This gives us the following XML:

<MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <MyChildOne SomeAttribute="test" SomethingElse="test" />
    <MyChildTwo>hello</MyChildTwo>
</MyRoot>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文