首页 新闻 会员 周边 捐助

C#解析xml数据

0
悬赏园豆:5 [待解决问题]

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的值呢?

 

Haydy的主页 Haydy | 初学一级 | 园豆:177
提问于:2013-06-28 15:54
< >
分享
所有回答(4)
0
XmlNodeList node = doc.GetElementsByTagName("Code");
foreach (XmlElement n in node)
{
string timeValue = n.InnerText;
}
WuRang | 园豆:1730 (小虾三级) | 2013-06-28 16:16
0

取下载个XmlHelper.cs 帮助类 直接调用就可以了 不过楼上的那个也可以

s_p | 园豆:140 (初学一级) | 2013-06-28 16:26
0
//你感受一下。
    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; }
    }
写代码的小2B | 园豆:4377 (老鸟四级) | 2013-06-28 16:57
0

正则也是个不错的选择~

只会造轮子 | 园豆:2274 (老鸟四级) | 2013-06-28 18:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册