How to read XML via c# such as

前端 未结 2 1320
孤独总比滥情好
孤独总比滥情好 2021-01-17 03:20

I want to know how to get attribute \"text\" via c#?

Example xml:



         


        
相关标签:
2条回答
  • 2021-01-17 03:50

    You could use XDocument and LINQ

    You'll need to include the System.Xml.Linq.XDocument namespace.

    Then you could do something like:

     XDocument document = XDocument.Load(filePath);
     var modes = (from modes in document.Root.Descendants("Mode")
                  select modes.Attribute("Name").Value).ToList();
    
    0 讨论(0)
  • 2021-01-17 04:02

    Like this:

    const string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <Model Name=""modelname"">
    <Mode Name=""text"">
    <Class>Class1</Class>
    </Mode>
    </Model>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    Console.WriteLine(doc.DocumentElement["Mode"].Attributes["Name"].Value);
    
    0 讨论(0)
提交回复
热议问题