0

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;
        }   
1
  • Your XML has several root elements. Your example code cannot work. It must throw an exception in the second line Commented May 15, 2015 at 9:49

2 Answers 2

1

Looking at the code labeled // Create Array it looks like you may want to move this line:

 int [] roleInt = roleIntList.Distinct().ToArray();

to be outside the foreach loop:

 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; 
Sign up to request clarification or add additional context in comments.

1 Comment

I've run your code. It doesn't compile, but after you make it compile it outputs {2, 1}. If you're getting that issue then your posted code doesn't match what you're running. I'd strongly suggest you change the approach, though.
1

First, if you're going to use an XML API then LINQ to XML XDocument is far better to work with than the creaking XmlDocument API.

Second, if you're just trying to get the XML into an object model then XmlSerializer is what you should be looking at. You will have very little code to write in comparison to manually parsing this:

[XmlRoot("Phrases")]    
public class Phrases : List<Phrase>
{

}

public class Phrase
{
    [XmlAttribute]
    public int Role { get; set; }
}

To deserialize your XML:

var serializer = new XmlSerializer(typeof(Phrases));

using (var reader = new StringReader(xml))
{
    var phrasesList = (Phrases)serializer.Deserialize(reader);
}

And to get your unique roles:

var uniqueRoles = phrasesList.Select(p => p.Role).Distinct();

I've assumed that your XML has a root node (your code above would throw an exception as it doesn't). For the example, I'm using Phrases, though if you add more information I can change this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.