【问题标题】:generating xml based on xsd schema (with .NET)基于 xsd 架构生成 xml(使用 .NET)
【发布时间】:2014-05-22 13:36:22
【问题描述】:

我想根据我的 xsd 架构 (cap.xsd) 生成一个 xml 文件。我找到了这篇文章并按照说明进行操作: Generating XML file using XSD file

  • 我在 xsd.exe 的帮助下创建了该类,并通过拖放将其插入到我的解决方案中
  • 之后,我构建了我的解决方案并创建了 xml。但它不基于 xsd 架构。
  • xml 文件有一个包含字符的元素,但架构说必须有数字(双)

  • 无论如何,我看不出 xsd 架构对生成的 xml 有什么影响?如果我删除架构,xml 文件仍然被创建。并且 xml 文件是在这一行创建的:

    var data = 新程序 { 时间 = "abc", 来源=“443543253243”, };

.. 而不是我的架构:

怎么了?


我的班级:

namespace testapp
{
    using System.IO;
    using System.Xml.Serialization;

    public class Program
    {
        public string Time;
        public string Source;

        public static void Main()
        {
            var data = new Program
                {
                    Time = "abc",
                    Source = "buffalo",
                };

            var serializer = new XmlSerializer(typeof(Program));
            using (var stream = new StreamWriter("E:\\cap_test.xml"))
            {
                serializer.Serialize(stream, data);
            }
        }
    }
}

我的架构:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="cap" type="capType"/>
    <xsd:complexType name="capType">
        <xsd:sequence>
            <xsd:element name="tel" type="telType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="telType">
        <xsd:sequence>
            <xsd:element name="time" type="xsd:double"/>
            <xsd:element name="source" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

还有我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<Program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Time>abc</Time>
    <Source>buffalo</Source>
</Program>

【问题讨论】:

    标签: c# .net xml xsd xml-serialization


    【解决方案1】:

    您应该使用从 xsd 生成的类,而不是使用 Program。当我跑步时

    xsd /classes schema.xsd
    

    它会创建一个schema.cs 文件。当我将它包含在我的项目中时,我可以编写以下代码:

    class Program
    {
        public static void Main()
        {
            var data = new capType { tel = new[] {
               new telType { source = "buffalo", time = 1 }
            } };
    
            var serializer = new XmlSerializer(typeof(capType));
            using (var stream = new StreamWriter(@"E:\cap_test.xml"))
            {
                serializer.Serialize(stream, data);
            }
        }
    }
    

    其中写道:

    <?xml version="1.0" encoding="utf-8"?>
    <cap xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <tel>
        <time>1</time>
        <source>buffalo</source>
      </tel>
    </cap>
    

    time 属性在schema.cs 中属于double 类型,这意味着您只能输入有效数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多