简体   繁体   中英

XML - Get Element Value - C#

I am trying to find if an element value exists then get a element value below it. ie

<Reply>
   <customer id="1223">
      <group>A</group>
      <class>
        <custclass>AB</custclass>
        <custval>1</custval>
      </class>
      <class>
        <custclass>CD</custclass>
        <custval>2</custval>
      </class>
   </customer>
</Reply>

I need to get the custval element value if the custclass = "CD". What is the best way to set this into a string in C# "2"? So I am looking for if custclass element value of "CD" exists then return the custval element value of 2.

Thanks for any info.

We can read the property value using various ways-

Method 1 - using XmlDocument

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList xnl = doc.SelectNodes("/Reply/customer/class");

foreach (XmlNode node in xnl)
{
    if (node.ChildNodes[0].InnerText == "CD")
    {
        Console.WriteLine(node.ChildNodes[1].InnerText);
    }
}

Method 2 - using XDcoument and LINQ

XDocument xml = XDocument.Load(xmlFile);
var result = xml.Root.DescendantsAndSelf("class")
        .Where(r => (string)r.Element("custclass").Value == "CD")
        .Select(s=> (string)s.Element("custval").Value).Single();
Console.WriteLine(result);

Method 3 - using XDocument and XPathSelectElement

var custval = xml.XPathSelectElement("Reply/customer/class[custclass='CD']/custval").Value;
Console.WriteLine(custval);

Method 4 - using XmlSerializer

Create C# classes using xmltocsharp & use Deserialize to convert the xml to object

[XmlRoot(ElementName = "class")]
public class Class
{
    [XmlElement(ElementName = "custclass")]
    public string Custclass { get; set; }
    [XmlElement(ElementName = "custval")]
    public string Custval { get; set; }
}

[XmlRoot(ElementName = "customer")]
public class Customer
{
    [XmlElement(ElementName = "group")]
    public string Group { get; set; }
    [XmlElement(ElementName = "class")]
    public List<Class> Class { get; set; }
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
}

[XmlRoot(ElementName = "Reply")]
public class Reply
{
    [XmlElement(ElementName = "customer")]
    public Customer Customer { get; set; }
}

static async System.Threading.Tasks.Task Main(string[] args)
{
    string xmlFile = @"xxxxxx.xml";
    using (StreamReader r = new StreamReader(xmlFile))
    {
        string xmlString = r.ReadToEnd();

        XmlSerializer ser = new XmlSerializer(typeof(Reply));

        using (TextReader reader = new StringReader(xmlString))
        {
            var result = (Reply)ser.Deserialize(reader);
            var custvalue = result.Customer.Class.Where(i => i.Custclass == "CD").Select(a => a.Custval).Single();
            Console.WriteLine(custvalue);
        }
    }
}

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