Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml online extract in textbox

Tags:

c#

xml

I have a xml file that is online with the tag <version>1.0</verion> and more, how can I extract the tag version and insert it into a textbox? the xml file is

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
like image 905
jolly Avatar asked Dec 12 '25 06:12

jolly


1 Answers

You did not provide the xml file. However the answer is simple.

Just use Linq to Xml and parse the file to get the value in version and whatever elements you need.

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SampleFile><version>1</version><SomeData>Hello World</SomeData></SampleFile>";

XDocument document = XDocument.Parse(xml);

string versionValue = document
    .Descendants("version")
    .Select(i => i.Value.ToString())
    .FirstOrDefault();

Console.WriteLine("The version is {0}", versionValue);

There was a comment which I think meant reading the xml document from a url. You should be able to use the XDocument.Load method.

This will work and pull an xml doc I found from a Google search at this location.

//var document = XDocument.Parse(xml);
var document = XDocument.Load("http://producthelp.sdl.com/SDL%20Trados%20Studio/client_en/sample.xml");


var versionValue = document
    .Descendants("version")
    .Select(i => i.Value.ToString())
    .FirstOrDefault();

Console.WriteLine("The version is {0}", versionValue);
like image 57
David Basarab Avatar answered Dec 14 '25 19:12

David Basarab