我正在将对象解析为xml文件。使用UBL标准。 在要序列化的类的某些部分中,它接受XmlElement。但是当我为这个领域设定价值时。当我序列化它时,结果文件,在这一部分,它再次包含模式定义,并且不尊重标记的前缀。
这是我的观点样本:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Invoice 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:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<sac:AdditionalInformation>
<sac:AdditionalMonetaryTotal>
<cbc:ID>1001</cbc:ID>
<cbc:PayableAmount currencyID="PEN">348199.15</cbc:PayableAmount>
</sac:AdditionalMonetaryTotal>
但这就是我得到的:
<?xml version="1.0" encoding="iso-8859-1"?>
<Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<AdditionalInformationType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns="">
<sac:AdditionalProperty>
<sac:AdditionalPropertyType xsi:type="sac:AdditionalMonetaryTotalType">
<sac:ID>1001</sac:ID>
<sac:PayableAmount currencyID="PEN">348199.15</sac:PayableAmount>
</sac:AdditionalPropertyType>
这是我用来设置XmlElement
:
XmlDocument xmlDoc = new XmlDocument();
ContaNet.Ubl.v2.Sac.AdditionalInformationType addInfo = new v2.Sac.AdditionalInformationType();
addInfo.AdditionalProperty.Add(new v2.Sac.AdditionalMonetaryTotalType() {
ID = new v2.Cbc.IDType() {
Value = "1001",
},
PayableAmount = new v2.Cbc.PayableAmountType() {
CurrencyID = v2.Udt.Enum.CurrencyCodeContentType.PEN,
Value = Convert.ToDecimal(348199.15)
}
});
using (XmlWriter xWriter = xmlDoc.CreateNavigator().AppendChild()) {
new XmlSerializer(addInfo.GetType()).Serialize(xWriter, addInfo);
}
invoice.UBLExtensions = new List<UBLExtensionType>();
UBLExtensionType extType = new UBLExtensionType();
extType.ExtensionContent = xmlDoc.DocumentElement;
invoice.UBLExtensions.Add(extType);
删除XmlElement中的模式定义的最佳方法是什么,并使用顶部定义的模式强制对象序列化。
这是我序列化对象的方式:
static void Main(string[] args) {
InvoiceType invoice = GenerateInvoice();
XmlWriterSettings setting = new XmlWriterSettings();
setting.ConformanceLevel = ConformanceLevel.Auto;
setting.Indent = true;
setting.IndentChars = "\t";
setting.Encoding = Encoding.GetEncoding("ISO-8859-1");
using (XmlWriter writer = XmlWriter.Create("Invoice.xml", setting)) {
Type typeToSerialize = typeof(InvoiceType);
XmlSerializer xs = new XmlSerializer(typeToSerialize);
xs.Serialize(writer, invoice);
}
Console.ReadLine();
}
XmlElement的原因是因为我可以存储任何内容
答案 0 :(得分:0)
如果您想存储任何内容,请使用object
和以下代码:
invoice.UBLExtensions = new List<object>();
invoice.UBLExtensions.Add(addInfo);
Type typeToSerialize = typeof(InvoiceType);
Type[] extraTypes = null;
if (invoice.UBLExtensions != null)
{ extraTypes = invoice.UBLExtensions.Select((e) => e.GetType()).ToArray(); }
// Now the serializer knows all additional types
XmlSerializer xs = new XmlSerializer(typeToSerialize, extraTypes);