gpt4 book ai didi

c# - 通过导入将 XSD 转换为 XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:37 25 4
gpt4 key购买 nike

我目前正在开发一种服务,用于生成遵循 UBL 发票标准的 XML 文件,因此我需要使用一些提供的 XSD 架构。

我正在使用 .NET C# 进行开发,并找到了一种可以将 XSD 映射到 C# 类的方法 - 通过使用 XSD.exe - 这似乎没问题?

我面临的问题是,xsd 文件中还有其他命名空间,这似乎对我生成的类 (xsd:imports) 造成了一些问题:

<!-- ===== xsd:schema Element With Namespaces Declarations ===== -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="2.0">
<!-- ===== Imports ===== -->
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" schemaLocation="../common/UBL-CommonAggregateComponents-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="../common/UBL-CommonBasicComponents-2.0.xsd"/>
<xsd:import namespace="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" schemaLocation="../common/UnqualifiedDataTypeSchemaModule-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" schemaLocation="../common/UBL-CommonExtensionComponents-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" schemaLocation="../common/UBL-QualifiedDatatypes-2.0.xsd"/>
<!-- ===== Root Element ===== -->
<xsd:element name="Invoice" type="InvoiceType">
...

我使用以下命令运行 xsd.exe:

xsd.exe /c C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\maindoc\UBL-Invoice-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonAggregateComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonBasicComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UnqualifiedDataTypeSchemaModule-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonExtensionComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-QualifiedDatatypes-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_UnitCode_UNECE_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_MIMEMediaTypeCode_IANA_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_LanguageCode_ISO_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_CurrencyCode_ISO_7_04.xsd

当我尝试使用生成的代码制作 XML 文件时,输出看起来像这样,其中缺少导入和 namespace 先行者,最终验证失败。

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<UBLVersionID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.0</UBLVersionID>
<LegalMonetaryTotal xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<LineExtensionAmount currencyID="DKK" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">200</LineExtensionAmount>
<PayableAmount currencyID="DKK" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">300</PayableAmount>
</LegalMonetaryTotal>
</Invoice>

这里是示例代码,我通过 xmlserializer 从生成的类生成 XML

XmlSerializer mySerializer = new XmlSerializer(typeof(InvoiceType));
InvoiceType invoice = new InvoiceType();

UBLVersionIDType UVer = new UBLVersionIDType();
UVer.Value = "2.0";
invoice.UBLVersionID = UVer;

MonetaryTotalType mtt = new MonetaryTotalType();
LineExtensionAmountType lep = new LineExtensionAmountType();
lep.currencyID = CurrencyCodeContentType.DKK;
lep.Value = 200;
PayableAmountType pat = new PayableAmountType();
pat.currencyID = CurrencyCodeContentType.DKK;
pat.Value = 300;

mtt.LineExtensionAmount = lep;
mtt.PayableAmount = pat;

invoice.LegalMonetaryTotal = mtt;
StreamWriter sw = new StreamWriter(@"C:\New folder\test2.xml");

mySerializer.Serialize(sw, invoice);
sw.Close();

我该如何解决这个问题,这是在 .NET 中制作遵循 xsd 模式的 XML 的正确(最佳/最简单)方法吗?

最佳答案

发现自己。

需要包含 XML 文档的命名空间和前缀的 XmlSerializerNamespaces 对象。

(http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx)

关于c# - 通过导入将 XSD 转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625949/

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