0

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

<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.

4
  • 2
    any code you have written? Commented Dec 19, 2019 at 13:55
  • so you want to see for all class leafs that have a custclass of CD what is the custvalue? Commented Dec 19, 2019 at 13:56
  • Try some XDocument-magic with XPAth. Commented Dec 19, 2019 at 13:57
  • Specifically, the XPath //customer/class[custclass='CD']/custval/text() will find what you want, when using XDocumentor or XmlDocument Commented Dec 19, 2019 at 14:00

1 Answer 1

2

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What happened to method 4?
@Hogan, Oops missed it.. added now
Thanks all this is a huge help I appreciate it.
@user2208746, you are welcome. please mark this as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.