gpt4 book ai didi

c# - 尝试使用 XDocument 和 XmlSchemaSet 从 xml 模式在 c# 中创建 xml

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:13 24 4
gpt4 key购买 nike

我正在尝试创建一个需要针对某些 xml 模式进行序列化的 xml 文档。这是现在的结果

 <?xml version="1.0" encoding="utf-8"?>
<StandardBusinessDocument>
<StandardBusinessDocumentHeader>
<HeaderVersion>1,0</HeaderVersion>
<Sender>
<Identifier>5790000011032</Identifier>
</Sender>
<Receiver>
<Identifier>5790000500000</Identifier>
</Receiver>
<DocumentIdentification>
<Standard>EAN.UCC</Standard>
<TypeVersion>2.8</TypeVersion>
<InstanceIdentifier>DI-35346-34535-xt435345</InstanceIdentifier>
<Type>catalogueItemNotification</Type>
<CreationDateAndTime>2013-12-20T10:46:26+00:00</CreationDateAndTime>
</DocumentIdentification>
</StandardBusinessDocumentHeader>
</StandardBusinessDocument>

它应该是这样的。

    <?xml version="1.0" encoding="utf-8"?>
<sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:eanucc="urn:ean.ucc:2" xmlns:gdsn="urn:ean.ucc:gdsn:2" xmlns:align="urn:ean.ucc:align:2" xmlns:chemical_ingredient="urn:ean.ucc:align:chemical_ingredient:2" xmlns:food_beverage_tobacco="urn:ean.ucc:align:food_beverage_tobacco:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader http://www.gdsregistry.org/2.8/schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CatalogueItemNotificationProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/AttributeValuePairExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CaseLevelNonGTINLogisticsUnitExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/TradeItemExtensionSpecificsProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/ChemicalIngredientExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/FoodAndBeverageTradeItemExtensionProxy.xsd">

<sh:StandardBusinessDocumentHeader>
<sh:HeaderVersion>1.0</sh:HeaderVersion>
<sh:Sender>
<sh:Identifier Authority="EAN.UCC">5790000011032</sh:Identifier>
</sh:Sender>
<sh:Receiver>
<sh:Identifier Authority="EAN.UCC">5790000500000</sh:Identifier>
</sh:Receiver>
<sh:DocumentIdentification>
<sh:Standard>EAN.UCC</sh:Standard>
<sh:TypeVersion>2.8</sh:TypeVersion>
<sh:InstanceIdentifier>DI-35346-34535-xt435345</sh:InstanceIdentifier>
<sh:Type>catalogueItemNotification</sh:Type>
<sh:CreationDateAndTime>2013-12-20T10:46:26+00:00</sh:CreationDateAndTime>
</sh:DocumentIdentification>
</sh:StandardBusinessDocumentHeader>
</sh:StandardBusinessDocument>

到目前为止,我在创建 xml 的方法中所拥有的是这个。

XmlSchemaSet sbdSchema = new XmlSchemaSet();
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\StandardBusinessDocumentHeader.xsd");
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\DocumentIdentification.xsd");
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\BasicTypes.xsd");
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\BusinessScope.xsd");
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\Manifest.xsd");
sbdSchema.Add("http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", "D:\\Europoultry\\Program\\EPWCF\\EPSystem\\EPSystem\\XMLSchemas\\sdbh\\Partner.xsd");

XDocument doc = new XDocument(
new XElement("StandardBusinessDocument",
new XElement("StandardBusinessDocumentHeader",
new XElement("HeaderVersion", "1,0"),
new XElement("Sender",
new XElement("Identifier", "5790000011032")),
new XElement("Receiver",
new XElement("Identifier", "5790000500000")),
new XElement("DocumentIdentification",
new XElement("Standard", "EAN.UCC"),
new XElement("TypeVersion", "2.8"),
new XElement("InstanceIdentifier", "DI-35346-34535-xt435345"),
new XElement("Type", "catalogueItemNotification"),
new XElement("CreationDateAndTime", "2013-12-20T10:46:26+00:00")
)))
);

var savePath = "C:\\GS1TradeSyncItem.xml";
doc.Save(savePath);

我不认为模式被导入是正确的,因为元素上没有一些属性,但我不确定这是否是问题所在。希望你们中的一个能帮忙。谢谢!

最佳答案

下面是我将如何添加命名空间

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 savePath = @"C:\temp\test.xml";
static void Main(string[] args)
{
string identification =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?> " +
"<sh:StandardBusinessDocument" +
" xmlns:sh=\"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader\"" +
" xmlns:eanucc=\"urn:ean.ucc:2\" " +
" xmlns:gdsn=\"urn:ean.ucc:gdsn:2\" " +
" xmlns:align=\"urn:ean.ucc:align:2\" " +
" xmlns:chemical_ingredient=\"urn:ean.ucc:align:chemical_ingredient:2\" " +
" xmlns:food_beverage_tobacco=\"urn:ean.ucc:align:food_beverage_tobacco:2\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
" xsi:schemaLocation=\"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader http://www.gdsregistry.org/2.8/schemas/sbdh/StandardBusinessDocumentHeader.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CatalogueItemNotificationProxy.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/AttributeValuePairExtensionProxy.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CaseLevelNonGTINLogisticsUnitExtensionProxy.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/TradeItemExtensionSpecificsProxy.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/ChemicalIngredientExtensionProxy.xsd" +
" urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/FoodAndBeverageTradeItemExtensionProxy.xsd\"" +
"/>";

XDocument doc = XDocument.Parse(identification);
XElement standardBusinessDocument = doc.Root;
XNamespace sh = standardBusinessDocument.Name.Namespace;

standardBusinessDocument.Add(
new XElement(sh + "StandardBusinessDocumentHeader",
new XElement(sh + "HeaderVersion", "1.0"),
new XElement(sh + "Sender",
new XElement(sh + "Identifier", new object[] {new XAttribute("Authority","EAN.UCC"), "5790000011032"})),
new XElement(sh + "Receiver",
new XElement(sh + "Identifier", new object[] {new XAttribute("Authority","EAN.UCC"), "5790000500000"})),
new XElement(sh + "DocumentIdentification",
new XElement(sh + "Standard", "EAN.UCC"),
new XElement(sh + "TypeVersion", "2.8"),
new XElement(sh + "InstanceIdentifier", "DI-35346-34535-xt435345"),
new XElement(sh + "Type", "catalogueItemNotification"),
new XElement(sh + "CreationDateAndTime", "2013-12-20T10:46:26+00:00")
))
);

doc.Save(savePath);
}
}
}

关于c# - 尝试使用 XDocument 和 XmlSchemaSet 从 xml 模式在 c# 中创建 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32607929/

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