, c#, xmldocument

    <VehicleID>83</VehicleID><VehName>4338 34ft Bounder</VehName>
    <VehicleID>84</VehicleID><VehName>4339 40ft Bounder</VehName>

i want to use xmldocument to loop with below condition.

string no = "4338"
string result = string.empty;
if (Left(<VehName>,4) =="4338")
result = value of the <vehicleID> innertext which is 83

How can I do that? get the value from the result?
Who is Participating?
Fernando SotoRetiredCommented:
Hi ITsolutionWizard;

This solutiion is using Linq to XML.

using System.Xml.Linq

// Load the XML document from the file syatem. Change the Path and Filename
// to where your is.
XDocument xdoc = XDocument.Load(@"C:\Working Directory\VehicleInfo.xml");
// Value you are looking for
string no = "4338";
// Find the VehicleID for the name
string result = (from name in xdoc.Descendants("VehName")
                 where name.Value.Contains(no)
                 select name.Parent.Element("VehicleID").Value).FirstOrDefault();

// See if the name was found and print the correct message
if( result != null )
    Console.WriteLine( "VehicleID = {0}", result);
    Console.WriteLine( "Name was not found.");

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.