gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:21 25 4
gpt4 key购买 nike

我正在尝试使用现有的 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制作了一个类
  • 我将类(class)添加到我的项目中
  • 编写了以下函数:

                    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>

谢谢!

最佳答案

我找到了答案:

  • 我不需要更改 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);

关于c# - 使用 XSD 文件在 C# 中生成 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com