之前写了从json中提取相应的标签值,这次是从xml中提取相应的标签值
/// <summary>
/// 从XML中提取相应的标签值
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
private string GetValueForXML(string tag, string xml)
{
string val = "";
if (xml != null && xml.Length > 0 && tag != null && tag.Length > 0)
{
try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xml);
System.Xml.XmlNode node = xmlDoc.SelectSingleNode("//" + tag);
val = node.InnerText;
}
catch (Exception ex)
{
}
if (string.IsNullOrEmpty(val))
{
int index = xml.IndexOf("<" + tag + ">");
if (index >= 0)
{
val = xml.Substring(index + tag.Length + 2);
index = val.IndexOf("</" + tag + ">");
if (index >= 0)
{
val = val.Substring(0, index);
val = val.Replace("<", "").Replace(">", "").Replace("/", "").Trim();
}
}
}
}
return val;
}