从 XPathNavigator.AppendChild() 创建时如何设置 XmlWriter.XmlWriterSettings?
我需要将 XmlWriter 的 XmlWriterSettings 的 OmitXmlDeclaration 属性设置为 false,以不创建 XML 声明。问题是我通过调用 XPathNavigator.AppendChild() 方法创建了 XmlWriter。下面的代码:
public String GetEntityXml<T>(List<T> entities)
{
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
ser.Serialize(writer, entities);
}
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
string resultString = stringWriter.ToString();
stringWriter.Close();
xmlTextWriter.Close();
return resultString;
}
知道如何序列化列表而不使用 XML 声明吗?
I need to set the OmitXmlDeclaration property of the XmlWriterSettings for a XmlWriter to false to not have XML declarations created. The issue is that I have created the XmlWriter from a call of a XPathNavigator.AppendChild() method. Code below:
public String GetEntityXml<T>(List<T> entities)
{
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
ser.Serialize(writer, entities);
}
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
string resultString = stringWriter.ToString();
stringWriter.Close();
xmlTextWriter.Close();
return resultString;
}
Any idea how to serialize the List and not have XML declarations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我执行你的代码时,我没有得到 XML 声明。序列化
List
会给出:请注意,
OmitXmlDeclaration
引用的“XML 声明”通常类似于:如果您引用的是
>xmlns
部分,那么这些被称为“XML 命名空间声明”,并且可以通过使用默认的空命名空间初始化XmlSerializerNamespaces
实例来消除,并且将其传递给您的Serialize
方法:下面是一个缩短的实现,它实现了与您的代码相同的结果:
I’m not getting the XML declaration when I execute your code. Serializing a
List<int>
gives me:Note that the “XML declaration” that
OmitXmlDeclaration
refers to is typically something similar to:If you’re instead referring to the
xmlns
parts, then those are called “XML namespace declarations”, and may be eliminated by initializing anXmlSerializerNamespaces
instance with a default empty namespace, and passing it to yourSerialize
method:The below is a shortened implementation which achieves the same result as your code:
尝试这种方法(为了便于阅读,切换到
var
):您可以创建
nav.AppendChild()
来创建XmlWriter
,而不是使用nav.AppendChild()
code>XmlWriter 单独使用,然后只需使用nav.AppendChild(string)
将 XML 写入xmlDoc
中。当您自己创建XmlWriter
时,可以省略 XML 声明。此外,在序列化时,您可能希望使用XmlSerializerNamespaces
类省略xmlns:xsi
和xmlns:xsd
命名空间。Try this approach (switched to
var
for readability):Rather than using
nav.AppendChild()
to create theXmlWriter
, you can create theXmlWriter
separately and then just usenav.AppendChild(string)
to write the XML intoxmlDoc
. When you create theXmlWriter
yourself, you can omit the XML declaration. Also, when you serialize, you'll probably want to omit thexmlns:xsi
andxmlns:xsd
namespaces using theXmlSerializerNamespaces
class.