0

I've got an XML file which has multiple entries, each looking like this:

-<CRQ>
 <ID>CRQ000000003314</ID>
 <Status>1</Status>
 <Summary>complete</Summary>
 <Service>Server</Service>
 <Impact>3000</Impact>
 <Risk>2</Risk>
 <Urgency>4000</Urgency>
 <Class>4000</Class>
 <Environment>1000</Environment>
 <Trigger/>
 <TriggerID>CP_00</TriggerID>
 <Coordinator>user name</Coordinator>
 <Desc>ticket description.</Desc>
</CRQ>                       

I have a string in a C# app which matches the id, eg CRQ000000003314. How would I be able to load the XML and then return the elements underneath the id (status summary etc) into separate text boxes when this string is matched on an event?

aperoll
  • 55
  • 1
  • 7
stefan
  • 439
  • 1
  • 10
  • 26

3 Answers3

1

You can get the element using LINQ to XML like this:

var xmlDocument = XDocument.Load("path");

var element = xmlDocument
            .Descendants("CRQ")
            .FirstOrDefault(x => (string) x.Element("ID") == "CRQ000000003314");

if(element != null)
{
    var status = (string)element.Element("Status");
    // get other values and display them in the textbox
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

There are a couple of ways to do this. You can use Linq to XML: http://msdn.microsoft.com/en-us/library/bb387061.aspx

Or you can use XMLReader:

http://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmlreader

IT is just the usual XML parsing with C#:

Reading data from XML

Community
  • 1
  • 1
Hozikimaru
  • 1,144
  • 1
  • 9
  • 20
0

This would be my way to do it :

//Added to set up the sample
var xml = "<root><CRQ><ID>CRQ000000003314</ID><Status>1</Status><Summary>complete</Summary><Service>Server</Service> <Impact>3000</Impact><Risk>2</Risk><Urgency>4000</Urgency><Class>4000</Class><Environment>1000</Environment><Trigger/><TriggerID>CP_00</TriggerID><Coordinator>user name</Coordinator><Desc>ticket description.</Desc></CRQ></root>";
XElement root = XElement.Parse(xml);

//Coded Solution
var value = (from el in root.Elements("CRQ") where el.Element("ID").Value == "CRQ000000003314" select el).FirstOrDefault();

//Testing output
foreach(XElement el in value.Elements()){
    Console.WriteLine("Element Name: {0} = {1}",el.Name, el.Value);
}
Dalorzo
  • 19,834
  • 7
  • 55
  • 102