0

我正在使用XmlTextWriter生成 xml 文件。大多数部分都很好,但遇到生成波纹管部分的问题,我需要的是:

<site isTrue="false">http://www.example.com</site>

我的部分主要代码:

...

using System.Xml;

string filePath = Application.StartupPath + "\\myxml.xml";
     XmlTextWriter myxml = null;
     try
     {
        myxml = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
        myxml.WriteStartDocument();
        // 
        // first 
        myxml.WriteElementString("site","http://www.example.com");
        //
        // second 
        //
        myxml.WriteStartElement("site")
        myxml.WriteAttributeString("isTrue", "false");
     }
    ...

然后,对于我尝试的第一种方法,结果是:

<site>http://www.example.com</site>

或者如果我使用第二个我尝试,那么结果将变为:

<site isTrue="false"></site>

添加属性和内文的任何方法?如下:

<site isTrue="false">http://www.example.com</site>

4

1 回答 1

0
myxml.WriteStartElement("site");
myxml.WriteAttributeString("isTrue", "false");
myxml.WriteString("http://www.example.com");
于 2014-06-23T08:30:53.050 回答