0

XML example :

<?xml version="1.0" encoding="utf-8" ?>
<brand name="brand1" num_brand="118" enabled="True">
  <price>
    <nodePattern>pattern</nodePattern>
    <attribute type="text" ></attribute>
    <treatment enabled="1" type="Regex">reg</treatment>
  </price>
  <title>
    <nodePattern>pattern</nodePattern>
    <attribute type="text" ></attribute>
    <treatment enabled="1" type="Regex">reg</treatment>
  </title>
</brand>

Please, how can I get the different attributes values and text for all my different nodes (for example name, num_brand and enabled for brand, enabled, type and "reg" for treatment) using System.Xml.Linq ?

Thank you !

Mat
  • 31
  • 1
  • 3
  • 1
    What have you tried so far? What are you struggling with? How do you want to get these values (in a single string? loaded into a class?) – Dan Field Dec 22 '15 at 18:36
  • The question now is what is your problem? Depending on what you want, Linq to XML is quite easy. e.g. `var x = doc.Root.Attribute("num_brand").Value;` gives you the text of that attribute.. – Stefan Dec 22 '15 at 18:40

3 Answers3

1

The System.Xml.Linq namespace is much nicer than the System.Xml namespace. Your XDocument has one XElement, which in turn has children elements. Each element has attributes and a value.

Here's an example for you:

var text = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <brand name=""brand1"" num_brand=""118"" enabled=""True"">
                <price>
                <nodePattern>pattern</nodePattern>
                <attribute type=""text"" ></attribute>
                <treatment enabled=""1"" type=""Regex"">reg</treatment>
                </price>
                <title>
                <nodePattern>pattern</nodePattern>
                <attribute type=""text"" ></attribute>
                <treatment enabled=""1"" type=""Regex"">reg</treatment>
                </title>
            </brand>";

XDocument document = XDocument.Parse(text);

// one root element - "brand"          
System.Diagnostics.Debug.Assert(document.Elements().Count() == 1);
XElement brand = document.Element("brand");

// brand has two children - price and title           
foreach (var element in brand.Elements())
    Console.WriteLine("element name: " + element.Name);

// brand has three attributes
foreach (var attr in brand.Attributes())
    Console.WriteLine("attribute name: " + attr.Name + ", value: " + attr.Value);
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
0

You have many ways to do that. One of them is the XmlDocument.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myXML);
foreach(XmlNode node in xmlDoc.DocumentElement.ChildNodes){
    string text = node.InnerText; //you can loop through children
}

Take a look on this post : How do I read and parse an XML file in C#?

Personnaly I like the Linq To Xml approach, more infos here : https://msdn.microsoft.com/en-us/library/bb387061.aspx

Community
  • 1
  • 1
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • 2
    Your answer suggests using `XmlDocument`, which is not using `System.Xml.Linq` (what the question is about). It also is specifying how to get the `InnerText` rather than attribute values... – Dan Field Dec 22 '15 at 18:56
0

try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENMAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENMAME);

            var brand = doc.Descendants("brand").Select(x => new
            {
                name = x.Attribute("name").Value,
                num_brand = x.Attribute("num_brand").Value,
                enabled = x.Attribute("enabled").Value,
                nodePattern = x.Element("price").Element("nodePattern").Value,
                attribute = x.Element("price").Element("attribute").Attribute("type").Value,
                priceTreatmentEnable = x.Element("price").Element("treatment").Attribute("enabled").Value,
                priceTreatmentType = x.Element("price").Element("treatment").Attribute("type").Value,
                priceTreatment = x.Element("price").Element("treatment").Value,
                titleTreatmentEnable = x.Element("title").Element("treatment").Attribute("enabled").Value,
                titleTreatmentType = x.Element("title").Element("treatment").Attribute("type").Value,
                titleTreatment = x.Element("title").Element("treatment").Value
            }).FirstOrDefault();
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20