C# 使用高亮显示将XML字符串格式化为友好的XML字符串
我使用它格式化XML字符串以打印友好的XML字符串C# 使用高亮显示将XML字符串格式化为友好的XML字符串,c#,xml,string,formatting,syntax-highlighting,C#,Xml,String,Formatting,Syntax Highlighting,我使用它格式化XML字符串以打印友好的XML字符串 public static string PrintXML(string xml) { string result = ""; MemoryStream mStream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode); XmlDocument document = new XmlD
public static string PrintXML(string xml)
{
string result = "";
MemoryStream mStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
XmlDocument document = new XmlDocument();
try
{
// Load the XmlDocument with the XML.
document.LoadXml(xml);
writer.Formatting = Formatting.Indented;
// Write the XML into a formatting XmlTextWriter
document.WriteContentTo(writer);
writer.Flush();
mStream.Flush();
// Have to rewind the MemoryStream in order to read
// its contents.
mStream.Position = 0;
// Read MemoryStream contents into a StreamReader.
StreamReader sReader = new StreamReader(mStream);
// Extract the text from the StreamReader.
string formattedXml = sReader.ReadToEnd();
result = formattedXml;
}
catch (XmlException)
{
// Handle the exception
}
mStream.Close();
writer.Close();
return result;
}
这真的很好用。但是我有一个问题,我想强调一下我的XML内容的一部分
问题如下:
我的字符串包含如下标题:STP/00/276/
因此:
string s=(“STP/00/276/GetInfo”)
如何解决这个问题
另一方面,是否可以更改内容的颜色
所以我想要这个:
<?xml version='1.0' encoding='iso-8859-1'?>
<RequestResponse>
<RequestName>GetInfo</RequestName>
</Params>
</RequestResponse>
获取信息
“GetInfo”应该用红色f.e.写。您只需先删除标题,用您已有的代码格式化其余部分,然后将标题添加回格式化的XML。大概是这样的:
string s = "STP/00/276/<?xml version='1.0' encoding='iso-8859-1'?><RequestResponse><Params><RequestName>GetInfo</RequestName></Params></RequestResponse>";
int index = s.IndexOf("<?xml");
string header = s.Substring(0, index);
s = s.Substring(index);
s = header + "\r\n" + PrintXML(s);
Console.WriteLine(s);
string s=“STP/00/276/GetInfo”;
int index=s.IndexOf(“您只需先删除头,用您已有的代码格式化其余部分,然后将头添加回格式化的XML。类似如下:
string s = "STP/00/276/<?xml version='1.0' encoding='iso-8859-1'?><RequestResponse><Params><RequestName>GetInfo</RequestName></Params></RequestResponse>";
int index = s.IndexOf("<?xml");
string header = s.Substring(0, index);
s = s.Substring(index);
s = header + "\r\n" + PrintXML(s);
Console.WriteLine(s);
string s=“STP/00/276/GetInfo”;
int index=s.IndexOf(“您只需先删除标题,用您已有的代码格式化其余部分,然后将标题添加回格式化的XML。这应该非常简单。如果标题不能包含旁注,请不要在字符串变量的值周围使用括号。为什么要这样做?使用XmlWriterSettings并设置属性标识=如果为true,则向数据添加返回值。@jdweng很抱歉这个问题可能很愚蠢,但如何使用XmLWriterSettings?XmLWriterSettings=new XmLWriterSettings();settings.Indent=true;MemoryStream mStream=new MemoryStream();XmlTextWriter tWriter=new XmlTextWriter(mStream,Encoding.Unicode);XmlWriter=XmlWriter.Create(twitter,设置);您只需先删除标题,用已有的代码格式化其余部分,然后将标题添加回格式化的XML。这应该很简单。如果标题不能包含旁注,请不要在字符串变量的值周围使用括号。为什么要这样做?使用XmlWriterSettings并设置属性标识=true以向数据添加返回值。@jdweng很抱歉这个问题可能很愚蠢,但如何使用XmLWriterSettings?XmLWriterSettings=new XmLWriterSettings();settings.Indent=true;MemoryStream mStream=new MemoryStream();XmlTextWriter tWriter=new XmlTextWriter(mStream,Encoding.Unicode);XmlWriter=XmlWriter.Create(twitter,设置);