xml源代码为:
<?xml version="1.0" encoding="utf-8" ?>
<APIResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Code>5000</Code>
<Result>查询成功</Result>
</APIResult>
我怎么获取Code和Result的值呢?
XmlNodeList node = doc.GetElementsByTagName("Code");
foreach (XmlElement n in node)
{
string timeValue = n.InnerText;
}
取下载个XmlHelper.cs 帮助类 直接调用就可以了 不过楼上的那个也可以
//你感受一下。 class Program { static void Main(String[] args) { var response = Serializer(new ApiResult() { Code = 5000, Result = "成功" }); var temp = Deserialize<ApiResult>(response); Console.WriteLine("Code:{0} Result:{1}", temp.Code, temp.Result); Console.ReadKey(); } private static T Deserialize<T>(String xml) { XmlSerializer xs = new XmlSerializer(typeof(T)); using (StringReader reader = new StringReader(xml)) { return (T)xs.Deserialize(reader); } } private static String Serializer(Object obj) { if (obj == null) return String.Empty; XmlSerializer xs = new XmlSerializer(obj.GetType()); using (MemoryStream ms = new MemoryStream()) { xs.Serialize(ms, obj); ms.Position = 0; using (StreamReader reader = new StreamReader(ms)) { return reader.ReadToEnd(); } } } } [XmlRoot] public class ApiResult { [XmlElement] public Int32 Code { get; set; } [XmlElement] public String Result { get; set; } }
正则也是个不错的选择~