07_05_GuyT
07_05_GuyT

Reputation: 2887

Get data from XML in simple way

I am using the following code to get data from the OData XML and its works , but I am not sure that I fully understand it so maybe there is a way to write it in simple way? what i need is to get the property value which is in the first loop value = 0001 and text = approve and in the second value = 0002 text = reject

The code

XNamespace dns = "http://schemas.microsoft.com/ado/2007/08/dataservices";
      if (response.StatusCode == HttpStatusCode.OK)
      {
         string decisionOptions = ReadResponse(response);
         XDocument document = XDocument.Parse(decisionOptions);
         foreach (XElement element in document.Element(dns + "DecisionOptions").Elements(dns + "element"))
        {
            PropertyKeyRef decisionOption = new PropertyKeyRef();
            decisionOption.PropertyValue = element.Element(dns + "DecisionKey").Value;
            decisionOption.PropertyName = element.Element(dns + "DecisionText").Value;
             dat.Add(decisionOption);

        }
      }

the XML

 <?xml version="1.0" encoding="utf-8" ?> 
- <d:DecisionOptions xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
- <d:element m:type="TAS.DecisionOption">
  <d:InstanceID>007</d:InstanceID> 
  <d:DecisionKey>0001</d:DecisionKey> 
  <d:DecisionText>Approve</d:DecisionText> 
  <d:CommentMandatory>false</d:CommentMandatory> 
  <d:Nature>POSITIVE</d:Nature> 
  </d:element>
- <d:element m:type="TAS.DecisionOption">
  <d:InstanceID>007</d:InstanceID> 
  <d:DecisionKey>0002</d:DecisionKey> 
  <d:DecisionText>Reject</d:DecisionText> 
  <d:CommentMandatory>true</d:CommentMandatory> 
  <d:Nature>NEGATIVE</d:Nature> 
  </d:element>
  </d:DecisionOptions>

Upvotes: 0

Views: 97

Answers (2)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13755

here how can do it in simple way using LINQ

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xdoc = XDocument.Load("test.xml");
            XNamespace dns = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            //in xml every element should have it's namespace for this reason  I have to concatenate namespace with the name of element  
            var elementsRes = xdoc.Root.Elements(dns+"element").Select((elt) => new PropertyKeyRef { PropertyName = elt.Element(dns+"DecisionKey").Value.ToString(),PropertyValue = elt.Element(dns+"DecisionText").Value.ToString() }).ToList();
            foreach (var item in elementsRes)
            {
                //your code for the result  
            }


        }
    }
    public  class PropertyKeyRef
    {
        public string PropertyName
        { get; set;  }
        public string PropertyValue
        { get; set; }
    }
}

Upvotes: 1

Rohit Vats
Rohit Vats

Reputation: 81233

You have already achieved it in simplest way. Little bit of LINQ might improve readability (get away with foreach loop) but it's just syntactic sugar of what you have written.

XNamespace dns = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XDocument document = XDocument.Load("database.xml");

PropertyKeyRef decisionOption = new PropertyKeyRef();
decisionOption.PropertyValue = document.Descendants(dns + "DecisionKey")
                                   .Select(node => node.Value).First();
decisionOption.PropertyName = document.Descendants(dns + "DecisionText")
                                   .Select(node => node.Value).First();

dat.Add(decisionOption);

Upvotes: 1

Related Questions