XMLTextWriter [英] XMLTextWriter

查看:102
本文介绍了XMLTextWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的每个人
我是XML新手,我想编写一个程序以这种格式在xml文件中写:


Dear everybody
I am new in XML, I want to write a program to write in an xml file in this format:


<?xml version="1.0" encoding="utf-8" ?>  
- <Orders>
  <Item id="1" SKU="998123" Description="Super Widget" Quantity="100" />
  <Item id="2" SKU="81312" Description="Turbo Flangellator" Quantity="50" />
  </Orders>



但是当我执行代码时,xml文件如下所示:



but when I execute the code the xml file appeared like the following:

- <Orders>
  <Item id="1" SKU="998123" Description="Super Widget" Quantity="100" />
  <Item id="2" SKU="81312" Description="Turbo Flangellator" Quantity="50" />
  </Orders>




我该怎么做才能让它写编码行?


我的代码是:




what should I do to let it write the encoding line?


my code is:

Dim xmlWriter As New XmlTextWriter("c:\XMLTextWriter.xml", Nothing)

With xmlWriter
    .WriteStartElement("Orders")
    .WriteStartElement("Item")
    .WriteAttributeString("id", "1")
    .WriteAttributeString("SKU", "998123")
    .WriteAttributeString("Description", "Super Widget")
    .WriteAttributeString("Quantity", "100")
    .WriteEndElement()

    .WriteStartElement("Item")
    .WriteAttributeString("id", "2")
    .WriteAttributeString("SKU", "81312")
    .WriteAttributeString("Description", "Turbo Flangellator")
    .WriteAttributeString("Quantity", "50")
    .WriteEndElement()


    .WriteEndElement()
    .Close()
End With

推荐答案

尝试一下-

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

这应该使您获得编码行
Try this -

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

This should get you the encoding line



试试看
Hi
Try this out
Dim enc As New System.Text.UTF8Encoding()
        Dim xmlWriter As New XmlTextWriter("c:\XMLTextWriter.xml", enc)

        With xmlWriter
            .WriteStartDocument()
            
            .WriteStartElement("Orders")
            .WriteStartElement("Item")
            .WriteAttributeString("id", "1")
            .WriteAttributeString("SKU", "998123")
            .WriteAttributeString("Description", "Super Widget")
            .WriteAttributeString("Quantity", "100")
            .WriteEndElement()

            .WriteStartElement("Item")
            .WriteAttributeString("id", "2")
            .WriteAttributeString("SKU", "81312")
            .WriteAttributeString("Description", "Turbo Flangellator")
            .WriteAttributeString("Quantity", "50")
            .WriteEndElement()


            .WriteEndElement()
            .Close()



您可以使用
格式化XML文件



You can format the XML file using

.Formatting =Formatting.Indented
.Indentation =3



希望这会对您有所帮助:)



Hope this will help you :)


我使用的是XmlDocument而不是XmlTextWriter.在这里,我正在使用 XmlDocument.CreateProcessingInstruction [ ^ ]方法.

干杯
Uwe
I am using XmlDocument instead of the XmlTextWriter. There, I am using the XmlDocument.CreateProcessingInstruction[^] method for this.

Cheers
Uwe


这篇关于XMLTextWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆