所以我有一个包含尖括号的长字符串,我希望从中提取文本部分.
string exampleString = "<1>text11><27>text2727><3>text33>";
我希望能够得到这个
1 = "text1" 27 = "text27" 3 = "text3"
我怎样才能轻松获得这个?我无法想出一个非黑客的方法来做到这一点.
谢谢.
使用基本XmlReader
和一些其他技巧来做包装来创建XML
类似的数据,我会做这样的事情
string xmlString = "<1>text11><27>text2727><3>text33>"; xmlString = "" + xmlString.Replace("<", " "; string key = ""; List > kvpList = new List >(); //assuming the result is in the KVP format using (XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString))){ bool firstElement = true; while (xmlReader.Read()) { if (firstElement) { //throwing away root firstElement = false; continue; } if (xmlReader.NodeType == XmlNodeType.Element) { key = xmlReader.Name.Substring(1); //cut of "o" } else if (xmlReader.NodeType == XmlNodeType.Text) { kvpList.Add(new KeyValuePair (key, xmlReader.Value)); } } }
编辑:
主要技巧是这一行:
xmlString = "" + xmlString.Replace("<", " "; //wrap to make this having single root, o is put to force the tagName started with known letter (comment edit suggested by Mr. chwarr)
当你第一次更换所有opening pointy brackets
带itself + char
,即
<1>text11> ->text1 //first replacement, fix the number issue
然后反转的所有序列opening point brackets + char + forward slash
以opening point brackets + forward slash + char
text1 -> text1 //second replacement, fix the ending tag issue
使用simple WinForm
with RichTextBox
打印出结果,
for (int i = 0; i < kvpList.Count; ++i) { richTextBox1.AppendText(kvpList[i].Key + " = " + kvpList[i].Value + "\n"); }
这是我得到的结果: