Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在序列化中禁用命名空间属性_C#_.net_Xml_Serialization - Fatal编程技术网

C# 在序列化中禁用命名空间属性

C# 在序列化中禁用命名空间属性,c#,.net,xml,serialization,C#,.net,Xml,Serialization,我使用以下代码来反序列化一个对象 之后,当我检查返回的节点时,我会得到两个额外的属性 {Attribute,Name=“xmlns:xsi”,Value=”http://www.w3.org/2001/XMLSchema-instance“}对象{System.Xml.xmldattribute} {Attribute,Name=“xmlns:xsd”,Value=”http://www.w3.org/2001/XMLSchema“}对象{System.Xml.xmldattribute} 我想

我使用以下代码来反序列化一个对象

之后,当我检查返回的节点时,我会得到两个额外的属性 {Attribute,Name=“xmlns:xsi”,Value=”http://www.w3.org/2001/XMLSchema-instance“}对象{System.Xml.xmldattribute} {Attribute,Name=“xmlns:xsd”,Value=”http://www.w3.org/2001/XMLSchema“}对象{System.Xml.xmldattribute}


我想知道如何禁用这两个属性来营救;一个简单(但完整)的示例:


为什么要“禁用”这些属性?他们不应该伤害任何东西。他们应该是一个假设性的陈述。我不希望我的元素中有额外的属性,这就是为什么.BCos我在xml中使用由不同组件读取的相同节点的原因。我希望确保它获得与回滚编辑版本所需的完全相同的输入。如果你需要编辑,请发表评论
        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;
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);
    }
}