如何将属性XML节点复制到指定的结构或数组

时间:2010-04-04 15:43:36

标签: c# xml

如何使用一个命令将属性XML节点复制到指定的结构或数组  例如

    public struct PossibilityJavamed
    {
        public string derv;
        public string dervt;
        public string num;
        public string gend;
    }
    PossibilityJavamed tmpstructnew = tmpstruct;
    ArrayList alstout = new ArrayList();// my array has some initial value 
    XmlNodeList nodeList;
    nodeList = docHarf.SelectNodes("//adatesmi");
            for (int i = 0; i < nodeList.Count; i++)
            {


                    tmpstructnew.derv = nodeList[i].Attributes["derv"].Value;
                    tmpstructnew.dervt = nodeList[i].Attributes["dervt"].Value;
                    tmpstructnew.num = nodeList[i].Attributes["num"].Value;
                    tmpstructnew.gend = nodeList[i].Attributes["gend"].Value;
                    alstout.Add(tmpstructnew);
            }

但我会在一个命令中完成

2 个答案:

答案 0 :(得分:2)

像这样:

alstout.AddRange(docHarf.SelectNodes("//adatesmi")
    .Select(n => new PossibilityJavamed {
        derv  = n.Attributes["derv"].Value,
        dervt = n.Attributes["dervt"].Value,
        num   = n.Attributes["num"].Value,
        gend  = n.Attributes["gend"].Value
    }));

答案 1 :(得分:2)

      alstout.AddRange(  (
                 from n in docHarf.SelectNodes("//adatesmi")
                 select new PossibilityJavamed(){
                    derv = n.Attributes["derv"].Value;
                    dervt = n.Attributes["dervt"].Value;
                    num = n.Attributes["num"].Value;
                    gend = n.Attributes["gend"].Value;
                 }
            ).ToList());