我正在尝试创建一个需要针对某些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);
我不认为模式是重要的,因为元素对它们没有一些属性,但我不确定这是不是问题所在。希望你们中的一个能提供帮助。 谢谢!
答案 0 :(得分:2)
以下是我将如何添加命名空间
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);
}
}
}