c#winform查询xml节点的内容 谁给个案例
发布网友
发布时间:2024-10-24 13:02
我来回答
共2个回答
热心网友
时间:2024-11-06 12:36
热心网友
时间:2024-11-06 12:34
/// <summary>
/// 解析xml字符
/// </summary>
/// <param name="xml">需要解析的字符串</param>
/// <param name="key">需要解析的字段</param>
/// <returns>返回值的集合string[]方式</returns>
public static string[] ParseXml(string xml, string key)
{
try
{
XmlDocument document = new XmlDocument();
document.LoadXml(xml.Trim());
XmlNodeList elements = document.GetElementsByTagName(key);
List<string> valueList = new List<string>();
foreach (XmlNode xmlNode in elements)
{
valueList.Add(xmlNode.InnerText);
}
if (valueList.Count <= 0)
{
return new string[] { "" };
}
return valueList.ToArray();
}
catch
{
return new string[] { "" };
}
}