返回单个字符串
static void Main(string[] args)
{
string xmlStr = xmlStr = "<ArrayOfString xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><string>fa141ca8-4ab7-48f0-8f39-d04b32b98273</string></ArrayOfString>";
ArrayOfString obj = DeserialiZe<ArrayOfString>(xmlStr);
Console.ReadKey();
}
/// <summary>
/// xml反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static T DeserialiZe<T>(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader reader = new StringReader(xml);
T result = (T)(xmlSerializer.Deserialize(reader));
reader.Close();
reader.Dispose();
return result;
}
//新建类
public class ArrayOfString
{
public string @string { get; set; }
}
返回字符串数组
public List<string> getXmlValue(string xml)
{
List<string> result = new List<string>();
XDocument xdoc = XDocument.Parse(xml); //加载xml
List<XElement> eles = new List<XElement>(); //获取符合条件的节点集合
//遍历节点,把符合条件的添加到 eles集合
foreach (XElement i in xdoc.Descendants().Where(e => {
if (e.Name.LocalName == "string")
{
return true;
}
return false;
}))
{
eles.Add(i);
}
foreach (XElement item in eles) //遍历查到的数据,输出
{
result.Add(item.Value);
}
return result;
}