Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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/2/jquery/75.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# 序列化对象时省略XML处理指令_C#_.net_Xml Serialization_Visual Studio 2003 - Fatal编程技术网

C# 序列化对象时省略XML处理指令

C# 序列化对象时省略XML处理指令,c#,.net,xml-serialization,visual-studio-2003,C#,.net,Xml Serialization,Visual Studio 2003,我正在序列化C#VS2003/.NET1.1应用程序中的一个对象。但是,我需要在没有处理指令的情况下序列化它。XmlSerializer类输出如下内容: <?xml version="1.0" encoding="utf-16" ?> <MyObject> <Property1>Data</Property1> <Property2>More Data</Property2> </MyObject&g

我正在序列化C#VS2003/.NET1.1应用程序中的一个对象。但是,我需要在没有处理指令的情况下序列化它。XmlSerializer类输出如下内容:

<?xml version="1.0" encoding="utf-16" ?> 
<MyObject>
    <Property1>Data</Property1>
    <Property2>More Data</Property2>
</MyObject>
在2.0中,您可以使用并序列化到XmlWriter,但我认为在1.1中不存在这种情况;所以这不是完全有用的-但还有一件“考虑升级”的事情。。。是的,我意识到这并不总是可能的。

如果“处理指令”指的是xml声明,那么可以通过设置XmlWriterSettings的OmitXmlDeclaration属性来避免这种情况。要实现这一点,您需要使用XmlWriter进行序列化

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

using ( XmlWriter stringWriter = new StringWriter(builder, settings) )
{
    serializer.Serialize(stringWriter, comments);
    return builder.ToString();
}

但是,这并不能回答你1.1的问题。好的,供其他人参考。

下面的链接将带您到一篇文章,其中有人有一种方法,通过使用XmlWriter,进入“元素”状态,而不是“开始”状态,来抑制处理指令。这会导致不写入处理指令

如果将XmlWriter传递给序列化程序,它将只发出一个处理消息 如果XmlWriter的状态为“开始”(即没有任何内容),则返回指令 还没有写信给它)

在这种情况下,写入程序将处于“元素”状态(元素内部) 因此,不会编写任何处理指令。一个你写完的 XML,您可以从底层流中提取文本并将其处理为 你心满意足


我做了一个小更正

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using ( XmlWriter stringWriter = XmlWriter.Create(builder, settings) )
{   
   serializer.Serialize(stringWriter, comments);  
  return builder.ToString();
}
这在.NET1.1中起作用。(但您仍然应该考虑升级)


省略名称空间怎么样

而不是使用

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add("", "");
例:



但这仍然需要对结果进行后处理才能删除MyContainingElement开始和结束标记,不是吗?虽然至少他们会被知道,所以这很好…我用WriteRaw(“”)替换了WriteStarteElement,并去掉了WriteEndElement-然后我需要做的就是修剪开始()BOM表。酷!
// Assume we have a type named 'MyType' and a variable of this type named 
'myObject' 
System.Text.StringBuilder output = new System.Text.StringBuilder(); 
System.IO.StringWriter internalWriter = new System.IO.StringWriter(output); 
System.Xml.XmlWriter writer = new System.Xml.XmlTextWriter(internalWriter); 
System.Xml.Serialization.XmlSerializer serializer = new 
System.Xml.Serialization.XmlSerializer(typeof(MyType)); 


writer.WriteStartElement("MyContainingElement"); 
serializer.Serialize(writer, myObject); 
writer.WriteEndElement(); 
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using ( XmlWriter stringWriter = XmlWriter.Create(builder, settings) )
{   
   serializer.Serialize(stringWriter, comments);  
  return builder.ToString();
}
    XmlSerializer s1= new XmlSerializer(typeof(MyClass)); 
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add( "", "" );


    MyClass c= new MyClass();
    c.PropertyFromDerivedClass= "Hallo";

    sw = new System.IO.StringWriter();
    s1.Serialize(new XTWND(sw), c, ns);
    ....

   /// XmlTextWriterFormattedNoDeclaration
   /// helper class : eliminates the XML Documentation at the
   /// start of a XML doc. 
   /// XTWFND = XmlTextWriterFormattedNoDeclaration
   public class XTWFND : System.Xml.XmlTextWriter
   {
     public XTWFND(System.IO.TextWriter w) : base(w) { Formatting = System.Xml.Formatting.Indented; }
     public override void WriteStartDocument() { }
   }
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add("", "");
<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">