尝试在c#中使用XDocument和XmlSchemaSet从xml模式创建xml
本文关键字:xml XmlSchemaSet XDocument 模式 创建 | 更新日期: 2023-09-27 18:12:54
我正在尝试创建一个需要针对某些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);
}
}
}