使用XSD文件在C#中生成XML文件

时间:2013-11-22 07:26:05

标签: c# xml xsd xml-serialization

我正在尝试使用现有的XSD文件使用C#构建新的XML文件。 这是xsd文件:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="KilometerUpload">
<xsd:complexType>
    <xsd:sequence>
        <xsd:element minOccurs="1" maxOccurs="unbounded" name="KilometerRegistration">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="ChassisNumber">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="17" />
                                <xsd:minLength value="1" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="KilometerStatus">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="7" />
                                <xsd:minLength value="1" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="TypeOfData">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="3" />
                                <xsd:minLength value="1" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="ObservationDate">
                        <xsd:annotation>
                            <xsd:documentation>Format: yyyyMMdd</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="8" />
                                <xsd:minLength value="8" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="LegallyResponsible">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="10" />
                                <xsd:minLength value="10" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="EnteredBy">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="10" />
                                <xsd:minLength value="10" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="InternalCode">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="10" />
                                <xsd:minLength value="0" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="DateFirstRegistration">
                        <xsd:annotation>
                            <xsd:documentation>Format: yyyyMMdd</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="8" />
                                <xsd:minLength value="0" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="Unifier">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="2" />
                                <xsd:minLength value="0" />
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="FeedbackType" type="FeedbackType" use="optional"/>
    <xsd:attribute name="FeedbackEmail" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="FeedbackType">
<xsd:annotation>
    <xsd:documentation>The feedback type for this file</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
    <xsd:enumeration value="FTP" />
    <xsd:enumeration value="EML" />
    <xsd:enumeration value="DEF" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

我已经完成了以下事情来创建XML文件:

  • 使用xsd.exe创建了一个类
  • 我将课程添加到我的项目
  • 写了以下函数:

                    var data = new KilometerUploadKilometerRegistration 
                {
                ChassisNumber = huidigefactuur.Wagen.Chassisnummer,
                KilometerStatus = huidigefactuur.KMStand.ToString(),
                TypeOfData = "120",
    
                };
                var serializer = new XmlSerializer(typeof(KilometerUploadKilometerRegistration));
                using (var stream = new StreamWriter("C:\\test.xml"))
                    serializer.Serialize(stream, data);
    

它正在创建XML文件,但我需要从KilometerUpload节点开始,而不是KilometerRegistration节点,我该怎么做?

这是我使用上面使用的代码获得的输出:

<?xml version="1.0" encoding="UTF-8"?>

-<KilometerUploadKilometerRegistration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ChassisNumber>WVWZZZ3CZ7E201402</ChassisNumber>

<KilometerStatus>78000</KilometerStatus>

<TypeOfData>120</TypeOfData>

</KilometerUploadKilometerRegistration>

谢谢!

4 个答案:

答案 0 :(得分:3)

我找到了答案:

  • 我不需要更改xsd.exe生成的类

这是我结束的代码,现在效果很好:

                foreach (Factuur huidigeFactuur2 in e.SelectedObjects)
            {
                XmlSerializer serializer2 = new XmlSerializer(typeof(KilometerUpload));
                TextWriter writer = new StreamWriter(@"C:\test2.xml");

                string chassisnummer = huidigeFactuur2.Wagen.Chassisnummer;
                string kilometerstatus = huidigeFactuur2.KMStand.ToString();

                KilometerUpload item = new KilometerUpload
                {
                    KilometerRegistration = new KilometerUploadKilometerRegistration[] { new KilometerUploadKilometerRegistration{ ChassisNumber = chassisnummer , TypeOfData = "120", KilometerStatus = kilometerstatus} },
                };

                serializer2.Serialize(writer, item);

答案 1 :(得分:1)

您的序列化程序位于KilometerUpdateKilometerRegistration您是否尝试将其设置为

var serializer = new XmlSerializer(typeof(kilometerUpload));

答案 2 :(得分:0)

编写一个xml文件,你可以使用XmlWriter类。

这是一个教程

http://www.dotnetperls.com/xmlwriter

答案 3 :(得分:0)

在代码中从xsd构建xml会很烦人。在过去,我使用了Priya Lakshminarayanan的XMLSampleGenerator。找到此特定项目的源代码here

我需要做你想做的事情,但是视觉工作室的样本生成还不够。我提供的链接的XmlSampleGenerator类似于VS样本生成,但显然你有源代码,因此你可以根据需要调试和调整它(在我的情况下,我需要正确的样本正则表达式生成)。

警告:从xsd生成xmls并非易事,所以不要指望它。祝好运。

编辑:此代码将生成一个Xml,其中还包含一个KilometerUpload节点。

namespace XmlGen {
public class Program {
    private static void Main( string[] args ) {

        XmlSerializer serializer = new XmlSerializer(typeof(XmlItem));
        TextWriter writer = new StreamWriter(@"C:\Users\hasch\Downloads\test.xml");
        XmlItem item = new XmlItem();
        serializer.Serialize(writer,item);
    }
}



public class XmlItem {

    public KilometerUpload KilometerUpload;

    public KilometerRegistration KilometerRegistration;

    public XmlItem() {
        KilometerUpload = new KilometerUpload();
        KilometerRegistration = new KilometerRegistration();
    }
}

public class KilometerUpload {


}

public class KilometerRegistration {

    public string ChassisNumber { get; set; }

    public string KilometerStatus { get; set; }

    public string TypeOfData { get; set; }

    public KilometerRegistration() {
        ChassisNumber = "WVWZZZ3CZ7E201402";

        KilometerStatus = "78000";

        TypeOfData = "120";

    }}}

这是生成的Xml:

<?xml version="1.0" encoding="utf-8"?> 
  <XmlItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema" >                    
<KilometerUpload />
 <KilometerRegistration>
  <ChassisNumber>WVWZZZ3CZ7E201402</ChassisNumber>
   <KilometerStatus>78000</KilometerStatus>
  <TypeOfData>120</TypeOfData>
</KilometerRegistration>

请记住,这实际上不是从xsd生成xml!如果您的xsd发生了变化,您将不得不对您的类进行重大更改等。