Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# 从xmlserializer中删除编码_C#_Xmlserializer - Fatal编程技术网

C# 从xmlserializer中删除编码

C# 从xmlserializer中删除编码,c#,xmlserializer,C#,Xmlserializer,我正在使用以下代码创建xml文档- XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); new XmlSerializer(typeof(docket)).Serialize(Console.Out, i, ns); 这在创建没有名称空间属性的xml文件时非常有效。我想在根元素中也没有编码属性,但是我找不到一种方法来做到这一点。有人知道这是否可以做到吗 谢谢删除旧答案并使用新解决方案更新: 假

我正在使用以下代码创建xml文档-

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
new XmlSerializer(typeof(docket)).Serialize(Console.Out, i, ns); 
这在创建没有名称空间属性的xml文件时非常有效。我想在根元素中也没有编码属性,但是我找不到一种方法来做到这一点。有人知道这是否可以做到吗


谢谢

删除旧答案并使用新解决方案更新:

假设完全删除xml声明是可以的,因为没有编码属性没有多大意义:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
using (XmlWriter writer = XmlWriter.Create(Console.Out, new XmlWriterSettings { OmitXmlDeclaration = true}))
{
  new XmlSerializer(typeof (SomeType)).Serialize(writer, new SomeType(), ns);
}
带编码的字符串;
使用(System.IO.MemoryStream memory=new System.IO.MemoryStream()){
使用(System.IO.StreamWriter writer=new System.IO.StreamWriter(内存)){
serializer.Serialize(writer,obj,null);
使用(System.IO.StreamReader=new System.IO.StreamReader(内存)){
记忆位置=0;
withEncoding=reader.ReadToEnd();
}
}
}
字符串withOutEncoding=withEncoding.Replace(“,”);
要从XML标头中删除编码,请使用null编码将TextWriter传递到XmlSerializer:

MemoryStream ms = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(ms, null);
s.Serialize(w, vs);
解释

XmlTextWriter使用传入构造函数的TextWriter的编码:

// XmlTextWriter constructor 
public XmlTextWriter(TextWriter w) : this()
{
  this.textWriter = w;
  this.encoding = w.Encoding;
  ..
它在生成XML时使用此编码:

// Snippet from XmlTextWriter.StartDocument
if (this.encoding != null)
{
  builder.Append(" encoding=");
  ...

感谢这个博客在代码方面的帮助

这是我的解决方案,同样的想法,但在VB.NET中,我的观点更清楚一点

Dim sw As StreamWriter = New, StreamWriter(req.GetRequestStream,System.Text.Encoding.ASCII)
Dim xSerializer As XmlSerializer = New XmlSerializer(GetType(T))
Dim nmsp As XmlSerializerNamespaces = New XmlSerializerNamespaces()
nmsp.Add("", "")

Dim xWriterSettings As XmlWriterSettings = New XmlWriterSettings()
xWriterSettings.OmitXmlDeclaration = True

Dim xmlWriter As XmlWriter = xmlWriter.Create(sw, xWriterSettings)
xSerializer.Serialize(xmlWriter, someObjectT, nmsp)

这与其说是一个恰当的回答,不如说是一个评论。。。试着写下你的答案,否则你可能会被否决。我同意,并且已经修改成了一个更好的答案这是可行的,但我想知道为什么它不能美化输出,而不是序列化文件流而不是使用字符串XmlWriterusing.Replace可以工作,但我想知道是否有一种方法可以使用xmlserializer来完成这项工作,就像我在上面删除名称空间所做的那样。我对同样的事情感兴趣。有人有答案吗?我使用了:StringBuilder.ToString().Replace(“,”);因为我有一些我不想编码的html代码。奇怪的是,阿希姆的回答导致了我序列化的其他错误。。。Create()的行为与“new XmlTextWriter()”的行为不同,并且给了我空引用错误。这项技术工作得很好,尽管如此,谢谢!使用
编码
(即
null
)是实现这一点的好方法。注意,根据XML,在XML声明中不包含编码声明是完全合法的。在这种情况下,XML声明如下所示:
(对于XML 1.0,则为
)。
Dim sw As StreamWriter = New, StreamWriter(req.GetRequestStream,System.Text.Encoding.ASCII)
Dim xSerializer As XmlSerializer = New XmlSerializer(GetType(T))
Dim nmsp As XmlSerializerNamespaces = New XmlSerializerNamespaces()
nmsp.Add("", "")

Dim xWriterSettings As XmlWriterSettings = New XmlWriterSettings()
xWriterSettings.OmitXmlDeclaration = True

Dim xmlWriter As XmlWriter = xmlWriter.Create(sw, xWriterSettings)
xSerializer.Serialize(xmlWriter, someObjectT, nmsp)