[英]disabling namespace attributes in serialization
正在使用以下代碼反序列化對象,
using (MemoryStream memoryStream = new MemoryStream()) { try { XmlWriterSettings writerSettings1 = new XmlWriterSettings(); writerSettings1.CloseOutput = false; writerSettings1.Encoding = System.Text.Encoding.UTF8; writerSettings1.Indent = false; writerSettings1.OmitXmlDeclaration = true;
XmlWriter writer1 = XmlWriter.Create(memoryStream, writerSettings1);
XmlSerializer xs1 = new XmlSerializer(obj.GetType(), string.Empty);
xs1.UnknownAttribute += new XmlAttributeEventHandler(xs1_UnknownAttribute);
xs1.UnknownElement += new XmlElementEventHandler(xs1_UnknownElement);
xs1.UnknownNode += new XmlNodeEventHandler(xs1_UnknownNode);
xs1.UnreferencedObject += new UnreferencedObjectEventHandler(xs1_UnreferencedObject);
xs1.Serialize(writer1, obj);
writer1.Close();
}
catch (InvalidOperationException)
{
return null;
}
memoryStream.Position = 0;
serializeObjectDoc.Load(memoryStream);
return serializeObjectDoc.DocumentElement;
之后,當我檢查返回節點時,我得到了兩個額外的屬性{Attribute,Name =“ xmlns:xsi”,Value =“ http://www.w3.org/2001/XMLSchema-instance”}對象{System.Xml。 XmlAttribute} {Attribute,Name =“ xmlns:xsd”,Value =“ http://www.w3.org/2001/XMLSchema”}對象{System.Xml.XmlAttribute}
我想知道如何禁用這兩個屬性
XmlSerializerNamespaces
解救; 一個簡單(但完整)的示例:
using System.Xml.Serialization;
using System;
public class Foo
{
public string Bar { get; set; }
static void Main()
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer ser = new XmlSerializer(typeof(Foo));
ser.Serialize(Console.Out, new Foo { Bar = "abc" }, ns);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.