带数据集的简单元素后输出xsd序列信息

Output xsd sequence information after simple element with dataset

我与 xsd.exe 合作,为 vb.net 创建 DataSet class。所有输出数据都包含在此 DataSet 中,因此我可以使用 DataSet.writexml(writer as XMLWriter) 方法将数据输出到文件。

本来我想按以下模式输出 xml 数据:

<customoutput>
    <zzz>Serial</zzz>
    <www1>Info1</www1>
    <www2>Info2</www2>
    <wwwn>Infon</wwwn>
</customoutput>

所以我想,描述 <www/> 序列是不可能的,因为节点名称必须是静态的。

出于这个原因,我决定尝试输出这个 XML:

<customoutput>
    <zzz>Serial</zzz>
    <www value="1">Info1</www>
    <www value="2">Info2</www>
    <www value="n">Infon</www>
</customoutput>

所以我创建了一个 XSD 文件来创建这个 xml。由于我缺乏 XSD 知识,我通过 Visual Studio 自动

做到了
        <xs:element name="customoutput">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="zzz" type="xs:string" />
              <xs:element maxOccurs="unbounded" name="www">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="value" type="xs:string" use="required" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>

奇怪的是:通过使用DataSet.writexml(writer as XMLWriter),我得到输出:

<customoutput>
    <zzz>Serial</zzz>
</customoutput>
<www value="1">Info1</www>
<www value="2">Info2</www>
<www value="n">Infon</www>

使用 xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;



namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XElement output = new XElement("customoutput", new object[] {
                new XElement("zzz", "Serial"),
                new XElement("www", new object[] { new XAttribute("value", 1), "Info1"}),
                new XElement("www", new object[] { new XAttribute("value", 2), "Info2"}),
                new XElement("www", new object[] { new XAttribute("value", "n"), "Infon"})
            });
            output.Save(FILENAME);
        }
    }
}

您的 XSD 会简单得多,您可以省略序列号。我会将复杂类型的内容定义为一个序列,并允许标记 'Info' 重复 (maxOccurs="unbounded")。 XML 模式序列是隐式排序的,因此不需要序列号。然后你可以使用你原来的技术,并分享 XSD.