0

I have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<keywords>
   <keyword subject="x">a</keyword>
   <keyword subject="y">b</keyword>
</keywords>

I would like to extract the node and attribute values from the above XML example like so:

a|x, b|y

Is this possible in C#?

4
  • You can use XmlReader to parse the XML and extract what is required. Commented Sep 2, 2019 at 14:22
  • I am sure I can thanks or I can use linq to xml. However, usually one can only extract node(s) values or attribute values. I am just not sure how to use, fro example, xmlreader to extract combinations like this ... Commented Sep 2, 2019 at 14:24
  • 1
    Possible duplicate of Get value of xml node in c# Commented Sep 2, 2019 at 14:26
  • @ZulqarnainJalil this is not related at all! Commented Sep 2, 2019 at 15:06

4 Answers 4

1

You could use LinqToXML to achieve your goal. Like this

XElement element = XElement.Parse(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                                    <keywords>
                                         <keyword subject=""x"">a</keyword>
                                         <keyword subject=""y"">b</keyword>
                                    </keywords>");

var result = String.Join(", ", (from d in element.Descendants("keyword")
                     select $"{d.Value}|{d.Attribute("subject").Value}"));

result string: a|x, b|y

This code sample is just demonstrating right direction and concept. Don't forget to implement all necessary checks and border cases (for example, is subject attribute required?).

Hope that helps.

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

1 Comment

@cs0815 was it helpful for you? If it was, please, mark as an answer
0

You can try using XElement to do that, ist as simpel as

XElement myXml = XElement.Load("xmlFile Location),

then you can use linq to query your dataSet, and just store the values inside some List that you can format into your desired output.

https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xelement?view=netframework-4.8 https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/xelement-class-overview

You can use the Attributes method to get an XAttribute back from the Element(your specific node) and its value

https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xattribute?view=netframework-4.8

Comments

0

You can use an XDocument for quick data retrieval (replace.Parse with .Load for a file path):

var document = XDocument.Parse(xml);
string result = document
                .Element("keywords")
                .Elements("keyword")
                .Aggregate<XElement, string>(string.Empty, (res, element)
                    => (string.IsNullOrEmpty(res) ? res : res + ", ") + element.Value + "|" + element.Attribute("subject").Value);

If the xml spec is yours you should probably make xml model classes and deserialize the xml into them.

Comments

0

Heres an answer using XmlReader class in C# Console app

 XmlReader reader = XmlReader.Create(@"[Location of your XML file here..]"); 
  while (reader.Read())
  {
    if (reader.NodeType == XmlNodeType.Element)
    {
      if (reader.Name.ToLower() == "keyword")
      {
        Console.WriteLine(reader.Value);

        if (reader.HasAttributes)
        {
          Console.WriteLine(reader.GetAttribute("subject"));
        }
      }
    }
  }
  Console.ReadKey();

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.