使用 XmlWriter 将 XML 输出到 ASP.NET TextBox 并进行格式化?

发布于 2024-10-20 04:08:48 字数 581 浏览 5 评论 0原文

我正在使用 XmlWriter 以编程方式构建 XML 字符串...

sb = New StringBuilder()
writer = XmlWriter.Create(sb)

writer.WriteStartDocument()
writer.WriteStartElement("root")

writer.WriteStartElement("element1")
writer.WriteAttributeString("myattribute", "myvalue")
writer.WriteEndElement()

writer.WriteEndElement()
writer.WriteEndDocument()

writer.Flush()

myTextBox.text = "<pre>" & Return sb.ToString() & "</pre>"

我希望能够将 XML 输出到 ASP.NET Web 表单上的 TextBox 控件。当我输出 XML 时,XML 中没有任何换行或缩进。 XmlWriter 似乎没有任何设置格式的属性。有没有办法保留格式和缩进?谢谢。

I am building an XML string programatically using the XmlWriter ...

sb = New StringBuilder()
writer = XmlWriter.Create(sb)

writer.WriteStartDocument()
writer.WriteStartElement("root")

writer.WriteStartElement("element1")
writer.WriteAttributeString("myattribute", "myvalue")
writer.WriteEndElement()

writer.WriteEndElement()
writer.WriteEndDocument()

writer.Flush()

myTextBox.text = "<pre>" & Return sb.ToString() & "</pre>"

I want to be able to output the XML to a TextBox control on a ASP.NET webform. When I do output the XML I don't get any line breaks or indentation in the XML. The XmlWriter does not seem to have any properties to set formatting. Is there anyway to retain formatting and indentation? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夕嗳→ 2024-10-27 04:08:48

使用 XmlWriterSettings 类实例指定缩进和换行符的使用,然后将其传递到 xmlWriter 的 Create 方法。请参阅此处的文档。

Use a XmlWriterSettings class instance to specify the use of indentations and line breaks and then pass it into the xmlWriter's Create method. See the documentation here.

玩物 2024-10-27 04:08:48

试试这个

  StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter();
        XmlTextWriter writer = new XmlTextWriter(sw);

        writer.Formatting = Formatting.Indented;


        // Write Xml Declaration with version 1.0 
        writer.WriteStartDocument();


        // Write Xml Document Root Element
        writer.WriteStartElement("Product");


        // Write attribute name and value to current Element 
        writer.WriteAttributeString("ProductID", "01");


        // Write Xml Element with Name and Inner Text
        writer.WriteElementString("ProductName", "P-Name");
        writer.WriteElementString("ProductQuantity","P-Quantity");
        writer.WriteElementString("ProductPrice", "P-Price");

        // Write Product Element closing Tag
        writer.WriteEndElement();

        // Write End of Document 
        writer.WriteEndDocument();

        // Flush and Close Writer
        writer.Flush();

        textBox1.Text = sw.ToString();

try this

  StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter();
        XmlTextWriter writer = new XmlTextWriter(sw);

        writer.Formatting = Formatting.Indented;


        // Write Xml Declaration with version 1.0 
        writer.WriteStartDocument();


        // Write Xml Document Root Element
        writer.WriteStartElement("Product");


        // Write attribute name and value to current Element 
        writer.WriteAttributeString("ProductID", "01");


        // Write Xml Element with Name and Inner Text
        writer.WriteElementString("ProductName", "P-Name");
        writer.WriteElementString("ProductQuantity","P-Quantity");
        writer.WriteElementString("ProductPrice", "P-Price");

        // Write Product Element closing Tag
        writer.WriteEndElement();

        // Write End of Document 
        writer.WriteEndDocument();

        // Flush and Close Writer
        writer.Flush();

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