0

I really need your help.

I wanted to read data from an XML file which content seems like that

<row>
    <Menue>949</Menue>
    <Text_D>Sonstige 49</Text_D>
    <Text_C>特别餐 49</Text_C>
    <Preis3>49</Preis3>
</row>
<row>
    <Menue>950</Menue>
    <Text_D>Sonstige 50</Text_D>
    <Text_C>特别餐 50</Text_C>
    <Preis3>50</Preis3>
</row>

I want to get the Text_D content by searching the Menue id. I tried so many ways now, I need to read it because other application will override this xml file and I need to keep my app updated.

My last "solution" was kinda good... but unfortunately the loading time will kill the whole system. It was an array to find the line of the id.

if(File.ReadLines(path).Skip(counterarray).Take(1).First().Contains(articlecode))
{
if not found - counter+=1;
if found - show...
}

hope you can help me this time!

2 Answers2

0

For ease consider using XDocument if it is available on your platform, which will load the whole file into memory in a parse tree and allow Linq queries to be performed on it, or XmlReader/XmlWriter if you want finer control

0

Use Linq to XML:

var doc = XDocument.Load(path);
string textD =
    (from row in doc.Root.Elements("row")
     let id = row.Element("Menue").Value
     where id == "949"
     select row.Element("Text_D").Value).FirstOrDefault();
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758