-1

Help please, I have the xml file,

<Users>
 <User FullName="" Name="sa">
  <userAvatarCache>-1</userAvatarCache>
  <description></description>
  <email></email>
  <phone></phone>
  <UserActive>true</UserActive>
 </User>

 <User FullName="asfgd" Name="stest">
  <userAvatarCache>-1</userAvatarCache>
  <description>aasad</description>
  <email>test@ukr.net</email>
  <phone>sdafdsfds0850</phone>
  <UserActive>true</UserActive>
 </User>
</Users>

I need to select FullName by email. I got all structure using

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(res.GetResponseStream());
 XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("User");

Now I need only 1 result (fullname) by email. How I can do it correct? Thanks

HelloWorld
  • 57
  • 3
  • 9
  • 1
    Parsing and working with XML is covered *extensively* in the documentation and all over the internet. I have complete confidence that you can find the answer, or find one you can adapt to your scenario, if you'd just look for it. What have you researched and what code do you have to achieve your goal here that isn't working? – tnw Dec 11 '14 at 21:15
  • 1
    And SO is full of similar question. Having only different xml-tags doesn't make your question unique and believe me, you are not the first one who needs to solve this kind of problem. – L.B Dec 11 '14 at 21:17

2 Answers2

4

Please google XPath, and try to use it. Note that using XmlDocument for large file is a very bad idea.

Code:

    var rawXml = @"<Users>
                 <User FullName="""" Name=""sa"">
                  <userAvatarCache>-1</userAvatarCache>
                  <description></description>
                  <email></email>
                  <phone></phone>
                  <UserActive>true</UserActive>
                 </User>

                 <User FullName=""asfgd"" Name=""stest"">
                  <userAvatarCache>-1</userAvatarCache>
                  <description>aasad</description>
                  <email>test@ukr.net</email>
                  <phone>sdafdsfds0850</phone>
                  <UserActive>true</UserActive>
                 </User>
                </Users>";

     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.LoadXml(rawXml);
     string email = "test@ukr.net";
     string selector = string.Format("User[email=\"{0}\"]", email);
     XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode(selector);

     if(xmlNode != null)
     {      
        string fullName = xmlNode.Attributes["FullName"].Value;

        Console.WriteLine(fullName);
     }
morfey
  • 71
  • 3
1

XPath to select element based on childs child value covers this quite well.

string xmlString = @"
 <Users>
  <User FullName='' Name='sa'>
   <userAvatarCache>-1</userAvatarCache>
   <description></description>
   <email></email>
   <phone></phone>
   <UserActive>true</UserActive>
  </User>
  <User FullName='asfgd' Name='stest'>
   <userAvatarCache>-1</userAvatarCache>
   <description>aasad</description>
   <email>test@ukr.net</email>
   <phone>sdafdsfds0850</phone>
   <UserActive>true</UserActive>
  </User>
 </Users>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("./User[email = 'test@ukr.net']");
Community
  • 1
  • 1
Zaxxon
  • 647
  • 5
  • 14
  • Thanks, but this is not exactly what I need, I can find Node by attribute, that contains something... I need by this email get FullName='asfgd' and Name='stest'.... and I trying do it correct :) not to take a string and parse that it is between quotes )) – HelloWorld Dec 11 '14 at 21:56
  • 1
    Vasyl's response includes retrieving the "FullName" attribute which you can then use to pull the additional "Name" element. He also separates the email out into it's own variable so it can be parameterized via a method. LINQ could also be used to solve this problem. – Zaxxon Dec 11 '14 at 22:10