C#XmlTextWriter删除标签

时间:2015-06-18 14:25:41

标签: c# xml asp.net-mvc

我正在尝试在我的ASP.NET MVC应用程序中显示XML,并且我正在使用XmlTextWriter类来执行此操作。我的代码:

string result = "";

MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode);
XmlDocument xmlDocument = new XmlDocument();

try
{
    // Load the XmlDocument with the XML.
    xmlDocument.LoadXml(xml);

    xmlTextWriter.Formatting = Formatting.Indented;

    // Write the XML into a formatting XmlTextWriter
    xmlDocument.WriteContentTo(xmlTextWriter);
    xmlTextWriter.Flush();
    memoryStream.Flush();

    // Have to rewind the MemoryStream in order to read its contents.
    memoryStream.Position = 0;

    // Read MemoryStream contents into a StreamReader.
    StreamReader streamReader = new StreamReader(memoryStream);

    // Extract the text from the StreamReader.
    String formattedXML = streamReader.ReadToEnd();

    result = formattedXML;
}
catch (XmlException e)
{
    //Print
}

这会在正确的缩进中输出XML,但问题是包围值的标记已经消失。例如: <test> this </test> 缺少<test> </test>个标签。我怎么能保留这些?

0 个答案:

没有答案