-3

I need to return the values for ID="3" and ID="4" to Labels

The xml string is stored in the database column exactly like this:

<Attributes><CheckoutAttribute ID="3"><CheckoutAttributeValue><Value>dear jason, wishing you a happy easter</Value></CheckoutAttributeValue></CheckoutAttribute><CheckoutAttribute ID="4"><CheckoutAttributeValue><Value>Thursday, 31-03-2016</Value></CheckoutAttributeValue></CheckoutAttribute></Attributes>

I'm looking to get the output as follows.

Label1.Text = "dear jason, wishing you a happy easter";

Label2.Text = "Thursday, 31-03-2016";

Label1 will always be for ID="3" Label2 will always be for ID="4"

Thanks

jasonkkt
  • 53
  • 4
  • 1
    Welcome to stackoverflow. This is a site for asking "what is wrong with what I have"... it is **not** for saying "please do code for me". Please find a tutorial (there are loads on the internet), give it a go... and when you're struggling, show us what you have (in a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve)) and we'll try and help. Please read the [ask] section in the help, and this [excellent article](http://whathaveyoutried.com). – freefaller Mar 25 '16 at 10:53
  • http://stackoverflow.com/questions/3750678/getting-attribute-value-of-an-xml-document-using-c-sharp – Fᴀʀʜᴀɴ Aɴᴀᴍ Mar 25 '16 at 10:54

1 Answers1

0

Try something like this to extract your strings....

Usings...

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

Classes...(created using your XML at http://xmltocsharp.azurewebsites.net/)

    [XmlRoot(ElementName = "CheckoutAttributeValue")]
    public class CheckoutAttributeValue
    {
        [XmlElement(ElementName = "Value")]
        public string Value { get; set; }
    }

    [XmlRoot(ElementName = "CheckoutAttribute")]
    public class CheckoutAttribute
    {
        [XmlElement(ElementName = "CheckoutAttributeValue")]
        public CheckoutAttributeValue CheckoutAttributeValue { get; set; }
        [XmlAttribute(AttributeName = "ID")]
        public string ID { get; set; }
    }

    [XmlRoot(ElementName = "Attributes")]
    public class Attributes
    {
        [XmlElement(ElementName = "CheckoutAttribute")]
        public List<CheckoutAttribute> CheckoutAttribute { get; set; }
    }

Code....

        string strXML = @"<Attributes>
                                <CheckoutAttribute ID=""3"">
                                    <CheckoutAttributeValue>
                                        <Value>dear jason, wishing you a happy easter</Value>
                                    </CheckoutAttributeValue>
                                </CheckoutAttribute>
                                <CheckoutAttribute ID=""4"">
                                    <CheckoutAttributeValue>
                                        <Value>Thursday, 31-03-2016</Value>
                                    </CheckoutAttributeValue>
                                </CheckoutAttribute>
                            </Attributes>";

        byte[] bufAttributes = ASCIIEncoding.UTF8.GetBytes(strXML);
        MemoryStream ms1 = new MemoryStream(bufAttributes);

        // Deserialize to object
        XmlSerializer serializerPlaces = new XmlSerializer(typeof(Attributes));
        try
        {
            using (XmlReader reader = new XmlTextReader(ms1))
            {
                Attributes deserializedXML = (Attributes)serializerPlaces.Deserialize(reader);

                string Label1Text = (from xmlTag in deserializedXML.CheckoutAttribute where xmlTag.ID == "3" select xmlTag.CheckoutAttributeValue.Value).FirstOrDefault();
                string Label2Text = (from xmlTag in deserializedXML.CheckoutAttribute where xmlTag.ID == "4" select xmlTag.CheckoutAttributeValue.Value).FirstOrDefault();

            }// put a break point here and mouse-over Label1Text and Label2Text ….
        }
        catch (Exception ex)
        {
            throw;
        }
Monty
  • 1,534
  • 2
  • 10
  • 12