简体   繁体   中英

How to get value of some object in XML string in C#?

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

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!

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;
             }
          }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM