0

I have this XML String :

<RESPONSE>
    <SINGLE>
        <KEY name="sitename">
            <VALUE>Stackoverflow</VALUE>
        </KEY>
        <KEY name="username">
            <VALUE>this value</VALUE>
        </KEY>
    </SINGLE>
</RESPONSE>

How to get value from Key that name "username" ? I want to get value of "this value" in my code. I try deserialized and any other code but it doesnt work. Please help me, thanks :D

Edit:

I tried using this code :

XDocument doc = XDocument.Load( "myXML.xml" );

var keys = doc.Descendants( "KEY" );

foreach ( var VALUE in keys )
{
    Console.WriteLine( VALUE.Value );
}

But how did I get the Value only from KEY that named "Username" ? Thanks :))

8
  • 1
    What are you doing so far? Commented Apr 15, 2014 at 12:26
  • take a look at this answer stackoverflow.com/questions/7119806/… Commented Apr 15, 2014 at 12:27
  • Normally you will try to get a Value from a concrete Key, in this case your are doing the opposite... Commented Apr 15, 2014 at 12:27
  • have you tried googling...? Commented Apr 15, 2014 at 12:27
  • I tried using this stackoverflow.com/questions/14722492/… and this stackoverflow.com/questions/3187444/… code but it doesnt work. I confuse because i must get an object inside of Value inside of Key inside of Single. How ? :" Commented Apr 15, 2014 at 12:28

3 Answers 3

3

You can probably use an xpath to do this. The following is an example XPath that will provide a node with name matching "sitename":

//KEY[@name="sitename"]

You can modify this slightly to find all nodes with a "name" attribute or to just find specific names. For more examples of how to use XPath see the MSDN site for XPath. The following is a snippet of C# code that shows you how to use this XPath (again, you can generalize for whatever XPath you need):

const string example_xml = "<RESPONSE> <SINGLE> <KEY name=\"sitename\"> <VALUE>Stackoverflow</VALUE> </KEY> <KEY name=\"username\"> <VALUE>this value</VALUE> </KEY> </SINGLE> </RESPONSE>";

// load
XmlDocument doc = new XmlDocument();
doc.LoadXml(example_xml);

// Query single or multiple nodes using the XPath, do what you want with this node!
var desiredNode = doc.SelectSingleNode("//KEY[@name=\"sitename\"]");

Best of luck!

Sign up to request clarification or add additional context in comments.

Comments

1

For completeness here is a System.Xml.Linq version, with the foreachs and where's being System.Linq for good measure. This basically the questioner's attempt, with a where to filter according to attribute.

const string example_xml = "<RESPONSE> <SINGLE> <KEY name=\"sitename\"> <VALUE>Stackoverflow</VALUE> </KEY> <KEY name=\"username\"> <VALUE>this value</VALUE> </KEY> </SINGLE> </RESPONSE>";

XDocument doc = XDocument.Parse(example_xml);
var keys = doc.Descendants("KEY");
var userKeys = keys.Where(item => item.Attribute("name").Value == "username").ToList();
userKeys.ForEach(item => Console.WriteLine(item.Value));

1 Comment

Sorry drew_w, I was lazy and nicked your string.
0

Lets consider your xml document as XYZ.xml, then you may try below code if you are using C#, below is the example only

       XmlDocument Doc = new XmlDocument();
        Doc.Load(Server.MapPath(".../xyz.xml"));
         XmlNodeList itemList = Doc.DocumentElement.SelectNodes("KEY");
         foreach (XmlNode currNode in itemList)
         {
             string name = string.Empty; 
             XmlNode item = currNode.SelectSingleNode("KEY");
             if(currNode["name"].InnerText == "username")//if you are aware of key name, use this       condition
             {
               name = item.Attributes["name"].Value.ToString(); // or currNode["name"].InnerText;
             }
          }

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.