tags:

views:

64

answers:

2

how to copy attribute XML node to specified structure or array with one command for example

    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);
            }

but i will do it in one command

+2  A: 

Like this:

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
    }));
SLaks
my array has some value before this function and with your answer lost initial value
jozi
You need to call `AddRange`. See my edit.
SLaks
The ToList() is in the wrong place. Infact, do you need it at all in this case?
Matti Virkkunen
@Matti: You're right; I forgot to remove it.
SLaks
+2  A: 
      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());
Nix
how to add tmpstructnew to array
jozi
my array has some value before this function and with your answer lost initial value
jozi
added addrange for you.
Nix