简体   繁体   中英

XML read uncomfortable values

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!

为方便起见,请考虑使用XDocument(如果您的平台可用),它将把整个文件加载到解析树中的内存中,并允许对它执行Linq查询;如果需要更好的控制,则可以使用XmlReader / XmlWriter。

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM