0

我正在解析一个 XML 文件,我想将解析后的值插入到数据库中,我解析了一组值并放在变量“数据”中。现在我想从变量中选择每个值。任何人都可以帮助我,我是新手吗?

编码

  XDocument xdoc = XDocument.Parse(e.Result);
  var data = from query in xdoc.Descendants("returnData")
                   select new fieldvalue
                   {
                       Authenticated = (String)query.Element("authenticated"),
                       Repphoto = (String)query.Element("rep_photo"),
                       Repuname = (String)query.Element("rep_uname"),
                       Repemail = (String)query.Element("rep_email"),
                       Repphone = (String)query.Element("rep_phone"),
                       Repwebsite = (String)query.Element("rep_website"),
                       Userimgsize = (String)query.Element("user_img_size"),
                       Usersigsize = (String)query.Element("user_sig_size")
                     };

类文件

public class fieldvalue
{
    String authenticated, rep_photo, rep_uname, rep_email, rep_phone, rep_website, user_img_size, user_sig_size;
  public String Authenticated
    {
        get { return authenticated; }
        set { authenticated = value; }
    }
    public String Repphoto
    {
        get { return rep_photo; }
        set { rep_photo = value; }
    }
    public String Repuname
    {
        get { return rep_uname; }
        set { rep_uname = value; }
    }
    public String Repemail
    {
        get { return rep_email; }
        set { rep_email = value; }
    }
    public String Repphone
    {
        get { return rep_phone; }
        set { rep_phone = value; }
    }
    public String Repwebsite
    {
        get { return rep_website; }
        set { rep_website = value; }
    }
    public String Userimgsize
    {
        get { return user_img_size; }
        set { user_img_size = value; }
    }
    public String Usersigsize
    {
        get { return user_sig_size; }
        set { user_sig_size = value; }
    }
}
}
4

1 回答 1

0

现在我想从变量中选择每个值。

IEnumerable<fieldvalue>结果转换为 ToList<>并使用索引来访问每个元素。

var data = (from query in xdoc.Descendants("returnData")
                   select new fieldvalue
                   {
                       Authenticated = (String)query.Element("authenticated"),
                       Repphoto = (String)query.Element("rep_photo"),
                       Repuname = (String)query.Element("rep_uname"),
                       Repemail = (String)query.Element("rep_email"),
                       Repphone = (String)query.Element("rep_phone"),
                       Repwebsite = (String)query.Element("rep_website"),
                       Userimgsize = (String)query.Element("user_img_size"),
                       Usersigsize = (String)query.Element("user_sig_size")
                     }).ToList();

就像是:

var name=data[0].Repuname;
于 2012-07-05T12:35:05.723 回答