从字符串格式化XML [英] XML formating from string

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

问题描述

Hello all,

在一些Web服务调用之后,我收到一个包含XML元素的String文本。基本上它是一个XML格式的SOAP响应,我想要

a)将它作为XML(具有正确的标识,标签,新行等)在消息框格式化中呈现< br> b)将它写在一个格式化的文本框上(在特定的文本框中写入的所有内容也都写在一个文件中,所以我希望txt文件能够以正确的方式格式化XML文档)
< br>我怎么能轻易做到这一点?我通过替换"><"来尝试了很多技巧。与"> \\\\ n<"但没有给出适当的气质......

有什么想法吗?非常感谢提前!

Hello all,

     After some web service invocation i am receiving a text as a String which contains XML elements. Essentially it is an XML formated SOAP response that i would like to 

a) present it in a message box formated  as an XML (with proper ident, tabs, new lines etc)
b) write it on a textBox formated as well (everything that is writing in a specific textBox is also being written in a file so i would like the txt file to get the XML document formated in the correct way)

    How can I easily do that? I 've tried a lot of tricks by replacing "><" with ">\r\n<" but does not give the proper alligment...

    Any ideas? many thanks in advance!

推荐答案

以下是一个示例ConsoleApplication,它显示了您想要获取的内容。取出您需要的东西,让它适合您的场景!请注意,此处没有异常检查,您可能希望在流操作周围添加一些。

Here is a sample ConsoleApplication that shows what you want to obtain. Take out what you need to make it work for your scenario! Note that there is no exception check here, you might want to add some around the stream operations.







< td> 使用系统;























































使用 System.IO;
使用 System.Text;
使用 System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
string soapMessage = "<?xml version = \" 1.0 \ " encoding = \" UTF-8 \ " standalone = \" no \ " ?>< SOAP-ENV:Envelope SOAP-ENV:encodingStyle = \" http: // schemas.xmlsoap.org/soap/encoding/\"的xmlns:SOAP-ENV = \" HTTP://schemas.xmlsoap.org/soap/envelope/\"的xmlns:SOAP-ENC = \" HTTP://schemas.xmlsoap.org/soap/encoding/\"的xmlns:的xsi = \" HTTP://www.w3.org/1999/XMLSchema-instance\"的xmlns:XSD = \" HTTP://www.w3.org/1999/XMLSchema\">< SOAP-ENV:身体与GT;< NS1:doubleAnInteger的xmlns:NS1 = \"瓮: MySoapServices\"><参数1的xsi:type = \" XSD:int\"> 123< /参数1>< / NS1:doubleAnInteger>< / SOAP-ENV:身体与GT;< / SOAP-ENV:信封>英寸;
XmlDocument xmlDocument = new XmlDocument的();
xmlDocument.LoadXml(soapMessage);
MemoryStream memoryStream = new MemoryStream();
UTF8Encoding utf8Encoding = new UTF8Encoding( false );
XmlTextWriter xmlTextWriter = new XmlTextWriter的(MemoryStream的,utf8Encoding);
xmlTextWriter.Formatting = Formatting.Indented;
xmlDocument.WriteContentTo(xmlTextWriter);
xmlTextWriter.Flush();
xmlTextWriter.Close();
Console.WriteLine(Encoding.UTF8.GetString(memoryStream.ToArray()));
Console.ReadLine();
}
}
}
using System;  
using System.IO;  
using System.Text;  
using System.Xml;  
 
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string soapMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"><SOAP-ENV:Body><ns1:doubleAnInteger xmlns:ns1=\"urn:MySoapServices\"><param1 xsi:type=\"xsd:int\">123</param1></ns1:doubleAnInteger></SOAP-ENV:Body></SOAP-ENV:Envelope>";  
 
            XmlDocument xmlDocument = new XmlDocument();  
            xmlDocument.LoadXml(soapMessage);  
 
            MemoryStream memoryStream = new MemoryStream();  
            UTF8Encoding utf8Encoding = new UTF8Encoding(false);  
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, utf8Encoding);  
            xmlTextWriter.Formatting = Formatting.Indented;  
            xmlDocument.WriteContentTo(xmlTextWriter);  
            xmlTextWriter.Flush();  
            xmlTextWriter.Close();  
 
            Console.WriteLine(Encoding.UTF8.GetString(memoryStream.ToArray()));  
            Console.ReadLine();  
        }  
    }  


这篇关于从字符串格式化XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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