从 XPathNavigator.AppendChild() 创建时如何设置 XmlWriter.XmlWriterSettings?

发布于 2025-01-04 18:49:48 字数 941 浏览 0 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2025-01-11 18:49:48

当我执行你的代码时,我没有得到 XML 声明。序列化 List 会给出:

<Int32_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <int>5</int>
  <int>7</int>
  <int>2</int>
</Int32_LIST>

请注意,OmitXmlDeclaration 引用的“XML 声明”通常类似于:

<?xml version="1.0" encoding="UTF-8" ?>

如果您引用的是 >xmlns 部分,那么这些被称为“XML 命名空间声明”,并且可以通过使用默认的空命名空间初始化 XmlSerializerNamespaces 实例来消除,并且将其传递给您的 Serialize 方法:

XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
ser.Serialize(writer, entities, namespaces);

下面是一个缩短的实现,它实现了与您的代码相同的结果:

public String GetEntityXml<T>(List<T> entities)
{
    var sb = new StringBuilder();
    var settings = new XmlWriterSettings { OmitXmlDeclaration = true };
    using (XmlWriter writer = XmlWriter.Create(sb, settings))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
        var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
        ser.Serialize(writer, entities, namespaces);
    }
    return sb.ToString();
}

I’m not getting the XML declaration when I execute your code. Serializing a List<int> gives me:

<Int32_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <int>5</int>
  <int>7</int>
  <int>2</int>
</Int32_LIST>

Note that the “XML declaration” that OmitXmlDeclaration refers to is typically something similar to:

<?xml version="1.0" encoding="UTF-8" ?>

If you’re instead referring to the xmlns parts, then those are called “XML namespace declarations”, and may be eliminated by initializing an XmlSerializerNamespaces instance with a default empty namespace, and passing it to your Serialize method:

XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
ser.Serialize(writer, entities, namespaces);

The below is a shortened implementation which achieves the same result as your code:

public String GetEntityXml<T>(List<T> entities)
{
    var sb = new StringBuilder();
    var settings = new XmlWriterSettings { OmitXmlDeclaration = true };
    using (XmlWriter writer = XmlWriter.Create(sb, settings))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(typeof(T).Name + "_LIST"));
        var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
        ser.Serialize(writer, entities, namespaces);
    }
    return sb.ToString();
}
流心雨 2025-01-11 18:49:48

尝试这种方法(为了便于阅读,切换到 var):

public String GetEntityXml<T>(List<T> entities)
{
    var xmlDoc = new XmlDocument();
    var nav = xmlDoc.CreateNavigator();
    using (var sw = new StringWriter())
    {
        //Create an XmlWriter that will omit xml declarations
        var s = new XmlWriterSettings{ OmitXmlDeclaration = true };
        using (var xmlWriter = XmlWriter.Create(sw, s))
        {
            //Use the following to serialize without namespaces
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            var ser = new XmlSerializer(typeof(List<T>),
                          new XmlRootAttribute(typeof(T).Name + "_LIST"));
            ser.Serialize(xmlWriter, entities, ns);
        }

        //Pass xml string to nav.AppendChild()
        nav.AppendChild(sw.ToString());
    }

    using (var stringWriter = new StringWriter())
    {
        using (var xmlTextWriter = XmlWriter.Create(stringWriter))
        {
            xmlDoc.WriteTo(xmlTextWriter);
        }
        return stringWriter.ToString();
    }
}

您可以创建 nav.AppendChild() 来创建 XmlWriter,而不是使用 nav.AppendChild() code>XmlWriter 单独使用,然后只需使用 nav.AppendChild(string) 将 XML 写入 xmlDoc 中。当您自己创建 XmlWriter 时,可以省略 XML 声明。此外,在序列化时,您可能希望使用 XmlSerializerNamespaces 类省略 xmlns:xsixmlns:xsd 命名空间。

Try this approach (switched to var for readability):

public String GetEntityXml<T>(List<T> entities)
{
    var xmlDoc = new XmlDocument();
    var nav = xmlDoc.CreateNavigator();
    using (var sw = new StringWriter())
    {
        //Create an XmlWriter that will omit xml declarations
        var s = new XmlWriterSettings{ OmitXmlDeclaration = true };
        using (var xmlWriter = XmlWriter.Create(sw, s))
        {
            //Use the following to serialize without namespaces
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            var ser = new XmlSerializer(typeof(List<T>),
                          new XmlRootAttribute(typeof(T).Name + "_LIST"));
            ser.Serialize(xmlWriter, entities, ns);
        }

        //Pass xml string to nav.AppendChild()
        nav.AppendChild(sw.ToString());
    }

    using (var stringWriter = new StringWriter())
    {
        using (var xmlTextWriter = XmlWriter.Create(stringWriter))
        {
            xmlDoc.WriteTo(xmlTextWriter);
        }
        return stringWriter.ToString();
    }
}

Rather than using nav.AppendChild() to create the XmlWriter, you can create the XmlWriter separately and then just use nav.AppendChild(string) to write the XML into xmlDoc. When you create the XmlWriter yourself, you can omit the XML declaration. Also, when you serialize, you'll probably want to omit the xmlns:xsi and xmlns:xsd namespaces using the XmlSerializerNamespaces class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文