I get a XML and I want to get the value of Role to make an array in C#, and then remove the same value to make another 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 is my start up, I get all the Role, but when I add them in to an array it doesn't show the value of the Role, just get System.Int32[]. The load xml and transform to 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 all the phrases
XmlNodeList phrases = xml.GetElementsByTagName("Phrase");
foreach (XmlNode phraseNode in phrases)
{
Phrase phrase = NodeToPhrase(phraseNode);
phrasesList.Add(phrase);
}
//Phrase node to 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 a phrase class
public Dictionary<string, Phrase> Phrases = new Dictionary<string, Phrase>();
public class Phrase
{
public string Role = null;
}
My create array and remove the same value code goes here:
//Create Array
List<int> roleIntList = new List<int>();
foreach(var roles in Phrases){
var roleNode = roles.Value as Phrase;
int roleNum = int.Parse(roleNode.Role);
roleIntList.Add(roleNum);//this can't get value 2,1,1,2,1,2;;
int [] roleInt = roleIntList.Distinct().ToArray();//this not work;
}