Faraaz bhilwade
Faraaz bhilwade

Reputation: 49

How to get the value of an attribute from a complex XML document in C# UWP?

XML file:

<?xml version="1.0"?> 
<DATA>
 <Element Name="Visualisie:ST_Visu_FilterdatenNeutral" Type="PvParameter">
  <Group ID="Visualisie:ST_Visu_FilterdatenNeutral">
   <Property ID="S_Kunde_Firma" DataType="STRING" Value="Biotest AG" />
   <Property ID="S_Gebaeude" DataType="STRING" Value="A" />
   <Property ID="S_Raum" DataType="STRING" Value="A003" />
   <Property ID="S_Anlage" DataType="STRING" Value="A003 Spuelbereich" />
   <Property ID="S_Filter_ID" DataType="STRING" Value="1046362" />
   <Property ID="S_Reinraumklasse" DataType="STRING" Value="GMP D" />
  </Group>
 </Element>
</DATA>

I tried it coding like down below but it wont work:

public async Task getdata()
{
    StorageFile file = await StorageFile.GetFileFromPathAsync(PathforNeutralFilterDatei);
    IRandomAccessStreamWithContentType stream = await file.OpenReadAsync();
    StreamReader rdr = new StreamReader(stream.AsStream(), Encoding.GetEncoding("ISO-8859-1"));
    var contents = rdr.ReadToEnd();
    XDocument doc = XDocument.Parse(contents);
    IEnumerable<string> skundename = from SKF in doc.Descendants("Property").
                                     Where(v => v.Attribute("ID").Value == "S_Kunde_Firma") 
                                     select SKF.Attribute("Value").Value;

    foreach (XElement SKF in skundename)
    { 
        string temp = SKF.Descendants("Property").Where(v => v.Attribute("ID").Value == "S_Kunde_Firma").ToString(); 
    }
}

I just want to get the value from the attribute 'Value' which is "Biotest AG" and save this string value in a public defined string (temp).

Upvotes: 0

Views: 242

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32785

how to get the value of an attribute from a complex XML document in C# UWP?

I checked your code, and the linq is correct, but the for-each segment is incorrect. Because the type of skundename is IEnumerable<string>. Please check the following code.

IEnumerable<string> skundename = from SKF in doc.Descendants("Property").
                                 Where(v => v.Attribute("ID").Value == "S_Kunde_Firma")
                                 select SKF.Attribute("Value").Value;
foreach (string item in skundename)
{
    string temp = item;
}

Upvotes: 0

jdweng
jdweng

Reputation: 34421

I like using dictionaries :

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

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

            Dictionary<string, string> dict = doc.Descendants("Property")
                .GroupBy(x => (string)x.Attribute("ID"), y => (string)y.Attribute("Value"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}

Upvotes: 1

Related Questions