开发者

How do I parse a word between two elements?and extract it

开发者 https://www.devze.com 2023-01-13 04:35 出处:网络
example <reference>employee开发者_开发百科</reference> <data>123</data> how to parse using c# so that i should get employee and 123 as outputYou can make an XML document ou

example

<reference>employee开发者_开发百科</reference>

<data>123</data>

how to parse using c# so that i should get employee and 123 as output


You can make an XML document out of it, and parse it:

string info = "<reference>employee</reference><data>123</data>";

XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + info + "</root>");
string reference = doc.DocumentElement.FirstChild.InnerText;
string data = doc.DocumentElement.FirstChild.NextSibling.InnerText;

Another option is to use a regular expression to parse the string:

string info = "<reference>employee</reference><data>123</data>";

MatchCollection m = Regex.Matches(info, "<.+?>(.+?)</.+?>");
string reference = m[0].Groups[1].Value;
string data = m[1].Groups[1].Value;

Or simple string manipulation:

string info = "<reference>employee</reference><data>123</data>";

int start = info.IndexOf("<reference>") + 11;
string reference = info.Substring(start, info.IndexOf('<', start) - start);
start = info.IndexOf("<data>") + 6;
string data = info.Substring(start, info.IndexOf('<', start) - start);


string xml = @"<root>
                    <reference>employee</reference> 
                    <data>123</data> 
                </root>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);

string employee = doc.SelectSingleNode("reference").InnerText;
string data = doc.SelectSingleNode("data").InnerText;
0

精彩评论

暂无评论...
验证码 换一张
取 消