-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>[email protected]</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

2
  • 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? Commented Dec 11, 2014 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. Commented Dec 11, 2014 at 21:17

2 Answers 2

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>[email protected]</email>
                  <phone>sdafdsfds0850</phone>
                  <UserActive>true</UserActive>
                 </User>
                </Users>";

     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.LoadXml(rawXml);
     string email = "[email protected]";
     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);
     }
Sign up to request clarification or add additional context in comments.

Comments

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>[email protected]</email>
   <phone>sdafdsfds0850</phone>
   <UserActive>true</UserActive>
  </User>
 </Users>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("./User[email = '[email protected]']");

2 Comments

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 ))
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.

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.