C# 这是一种效率低下的XML解析方法吗?

C# 这是一种效率低下的XML解析方法吗?,c#,.net,xml,webclient,C#,.net,Xml,Webclient,我可能在担心错误的优化,但我有一种烦人的想法,那就是它在一次又一次地解析xml树,也许我在什么地方读到过它。不记得了 不管怎么说,我是这么做的: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.Net; namespace LinqTestingGrounds { class Program

我可能在担心错误的优化,但我有一种烦人的想法,那就是它在一次又一次地解析xml树,也许我在什么地方读到过它。不记得了

不管怎么说,我是这么做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;

namespace LinqTestingGrounds
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri("http://www.dreamincode.net/forums/xml.php?showuser=335389"));
            Console.ReadLine();
        }

        static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }

            XDocument xml = XDocument.Parse(e.Result);

            User user = new User();
            user.ID = xml.Element("ipb").Element("profile").Element("id").Value;
            user.Name = xml.Element("ipb").Element("profile").Element("name").Value;
            user.Rating = xml.Element("ipb").Element("profile").Element("rating").Value;
            user.Photo = xml.Element("ipb").Element("profile").Element("photo").Value;
            user.Reputation = xml.Element("ipb").Element("profile").Element("reputation").Value;
            user.Group = xml.Element("ipb").Element("profile").Element("group").Element("span").Value;
            user.Posts = xml.Element("ipb").Element("profile").Element("posts").Value;
            user.PostsPerDay = xml.Element("ipb").Element("profile").Element("postsperday").Value;
            user.JoinDate = xml.Element("ipb").Element("profile").Element("joined").Value;
            user.ProfileViews = xml.Element("ipb").Element("profile").Element("views").Value;
            user.LastActive = xml.Element("ipb").Element("profile").Element("lastactive").Value;
            user.Location = xml.Element("ipb").Element("profile").Element("location").Value;
            user.Title = xml.Element("ipb").Element("profile").Element("title").Value;
            user.Age = xml.Element("ipb").Element("profile").Element("age").Value;
            user.Birthday= xml.Element("ipb").Element("profile").Element("birthday").Value;
            user.Gender = xml.Element("ipb").Element("profile").Element("gender").Element("gender").Element("value").Value;

            Console.WriteLine(user.ID);
            Console.WriteLine(user.Name);
            Console.WriteLine(user.Rating);
            Console.WriteLine(user.Photo);
            Console.WriteLine(user.Reputation);
            Console.WriteLine(user.Group);
            Console.WriteLine(user.Posts);
            Console.WriteLine(user.PostsPerDay);
            Console.WriteLine(user.JoinDate);
            Console.WriteLine(user.ProfileViews);
            Console.WriteLine(user.LastActive);
            Console.WriteLine(user.Location);
            Console.WriteLine(user.Title);
            Console.WriteLine(user.Age);
            Console.WriteLine(user.Birthday);
            Console.WriteLine(user.Gender);

            //Console.WriteLine(xml);            
        }
    }
}
这够好吗™ 或者有没有更快的方法来解析我需要的东西


另外,我正在DownloadStringCompleted事件中执行大部分操作,我是否应该这样做?第一次使用这种方法。谢谢

不知道效率,但为了可读性,请使用
profile
变量,而不是反复遍历整个过程:

 User user = new User();
 var profile = xml.Element("ipb").Element("profile");
 user.ID = profile.Element("id").Value;

除奥德的答复外,我还补充说: 另一种提高可读性的方法是使用扩展方法

因此,您的代码如下所示:

user.ID = xml.XPathSelectElement("ipb/profile/id").Value;

我相信xml序列化是解决这类问题的方法。只要您的属性与xml元素匹配,这将是微不足道的。否则,您只需要使用XmlElement和XmlAttribute属性类来映射它们。下面是一些用于将普通xml反序列化到类中的简单代码:

public T Deserialise(string someXml)
    {   
        XmlSerializer reader = new XmlSerializer(typeof (T));
        StringReader stringReader = new StringReader(someXml);
        XmlTextReader xmlReader = new XmlTextReader(stringReader);
        return (T) reader.Deserialize(xmlReader);
    }

不,它不是反复解析XML;只要一次,当你打电话的时候

XDocument.Parse(e.Result);
之后的调用只访问xml对象中的树结构

“解析”意味着分析非结构化文本字符串(例如来自文件)并从中创建数据结构(例如树)。您的
。元素(“foo”)
调用不是解析,而是访问由
XDocument.Parse()调用构建的数据结构的一部分


如果您想知道您的代码是否冗余地重复了一些步骤,并且可以进行优化,那么是的,您正在冗余地遍历
ipb/profile
。这不是解析,但对元素(“foo”)的调用确实需要将字符串参数与子元素的名称进行比较@Oded的建议出于可读性的原因解决了这一问题,但也有助于提高效率。

啊,是的,将所有概要文件元素分组,并使用该元素解析我需要的内容。这会降低字符数,所以谢谢!好主意,但可能更适合遍历集合(即获取用于迭代的节点集),而不是此处的特定需求(获取特定节点值)。