开发者

Getting a value of a key from an XML file in C#

开发者 https://www.devze.com 2023-03-02 19:18 出处:网络
I have an XML file with the following structure: <Definitions> <Definition Execution=\"Firstkey\" Name=\"Somevalue\"></Definition>

I have an XML file with the following structure:

<Definitions>
  <Definition Execution="Firstkey" Name="Somevalue"></Definition>
  <Definition Execution="Secondkey" Name="Someothervalue"></Definition>
</Definitions>

How can I get开发者_如何学Go the values of the keys (Firstkey, Secondkey) and write them down using C# in my .NET application?

Thanks.


Using Linq to XML this is straightforward.

To just get the keys:

var keys = doc.Descendants("Definition")
              .Select(x => x.Attribute("Execution").Value);
foreach (string key in keys)
{
    Console.WriteLine("Key = {0}", key);
}

To get all values:

XDocument doc = XDocument.Load("test.xml");
var definitions = doc.Descendants("Definition")
                     .Select(x => new { Execution = x.Attribute("Execution").Value,
                                        Name = x.Attribute("Name").Value });
foreach (var def in definitions)
{
    Console.WriteLine("Execution = {0}, Value = {1}", def.Execution, def.Name);
}

Edit in response to comment:

I think what you really want is a dictionary, that maps from a key ("Execution") to a value ("Name"):

XDocument doc = XDocument.Load("test.xml");
Dictionary<string, string> dict = doc.Descendants("Definition")
                                     .ToDictionary(x => x.Attribute("Execution").Value, 
                                                   x => x.Attribute("Name").Value);
string firstKeyValue = dict["Firstkey"]; //Somevalue


using System.Xml.Linq;

var keys = XDocument.Load("path to the XML file")
.Root
.Elements()
.Select(x => x.Attribute("Execution"));


Using XMLDocumnet/XMLNode(s):

//Load the XML document into memory
XmlDocument doc = new XmlDocument();
doc.Load("myXML.xml");

//get a list of the Definition nodes in the document
XmlNodeList nodes = doc.GetElementsByTagName("Definition");

//loop through each node in the XML
foreach (XmlNode node in nodes)
{
   //access the key attribute, since it is named Execution,
   //that is what I pass as the index of the attribute to get
   string key = node.Attributes["Execution"].Value;

   //To select a single node, check if we have the right key
   if(key == "SecondKey") //then this is the second node
   {
      //do stuff with it
   }
}

basically you load the xml into a document variable, select the nodes you wish to view. Then iterate through them and store pertinent information.


XPath would be a great choice, I'd say.

Below is a sample XPath expression.

//Definition[@Execution='Firstkey']/@Name

As a XPath expression is a string, you can easily replace 'Firstkey' with whatever you need.

Use this with a XmlDocument.SelectSingleNode or XmlDocument.SelectNodes method

Both the above mentioned methods return an XmlNode. You can easily access the XmlNode.Value

Here are some XPath expressions

Don't forget XPath Visualizer which makes working with XPath so much easier!

0

精彩评论

暂无评论...
验证码 换一张
取 消