Array in C# with data of XML -
i xml , want value of role make array in c#, , remove same value make one.xml goes here:
<phrase role="2"></phrase> <phrase role="2"></phrase> <phrase role="1"></phrase> <phrase role="1"></phrase> <phrase role="2"></phrase> <phrase role="1"></phrase> <phrase role="2"></phrase>
here start up, role, when add them in array doesn't show value of role, system.int32[]. load xml , transform class code goes here:
xmldocument doc = new xmldocument(); doc.loadxml("<phrase role="2"></phrase> <phrase role="2"></phrase> <phrase role="1"></phrase> <phrase role="1"></phrase> <phrase role="2"></phrase> <phrase role="1"></phrase> <phrase role="2"></phrase>"); //get phrases xmlnodelist phrases = xml.getelementsbytagname("phrase"); foreach (xmlnode phrasenode in phrases) { phrase phrase = nodetophrase(phrasenode); phraseslist.add(phrase); } //phrase node phrase private phrase nodetophrase(xmlnode node) { phrase phrase = new phrase(); xmlnode rolenode = node.attributes["role"]; if (rolenode != null && !string.isnullorempty(rolenode.value)) phrase.role = rolenode.value; return phrase; } //crate phrase class public dictionary<string, phrase> phrases = new dictionary<string, phrase>(); public class phrase { public string role = null; }
my create array , remove same value code goes here:
//create array list<int> roleintlist = new list<int>(); foreach(var roles in phrases){ var rolenode = roles.value phrase; int rolenum = int.parse(rolenode.role); roleintlist.add(rolenum);//this can't value 2,1,1,2,1,2;; int [] roleint = roleintlist.distinct().toarray();//this not work; }
looking @ code labeled // create array looks may want move line:
int [] roleint = roleintlist.distinct().toarray();
to outside foreach loop:
list<int> roleintlist = new list<int>(); foreach(var roles in phrases){ var rolenode = roles.value phrase; int rolenum = int.parse(rolenode.role); roleintlist.add(rolenum);//this can't value 2,1,1,2,1,2;; } int [] roleint = roleintlist.distinct().toarray();//this not work;
Comments
Post a Comment