Extracto en línea de XML en el cuadro de texto

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\"?>"

preguntado el 28 de agosto de 11 a las 02:08

Your question is a bit confusing... Did you want the version out of the Xml declaration (<?xml version="1.0" ...), or did you want to get the version out of a version element in the body of the document (<version>1.0</version>)? -

1 Respuestas

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

Sólo tiene que utilizar Linq a 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 método.

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

//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);

Respondido 28 ago 11, 08:08

@jolly what do you mean by the url the xml file? Are you saying the xml file is a url? That it is not a string? - David Basarab

+1 because I didn't realize Uris would work :) Some nit-picks: Descendants("Version"), if the version attribute weren't specified, would grab any version element or attribute inside the document. Since the version is supposed to be specified in the Xml declaration, shouldn't the search be limited to that location? Also note that if version isn't specified at all, you'll print out: "The version is ". - Merlyn Morgan-Graham

Hmm, nevermind that first part. The question is ambiguous because it shows an element with a version, then the version in the Xml declaration... - Merlyn Morgan-Graham

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.