0

I have a request that returns a large xml file. I have the file in a XmlDocument type in my application. From that Doc how can I read an element like this:

<gphoto:videostatus>final</gphoto:videostatus>

I would like to pull that value final from that element. Also If i have multiple elements as well, can I pull that into a list? thanks for any advice.

4
  • Is that all you want from the XML file? Just the gphoto:videostatus elements? Commented Apr 3, 2012 at 4:58
  • @RobertHarvey, Yes thats one thing I want. And also I also want to pull out a list of elements with the same "element name" such as <link>link1</link><link>link2></link> Commented Apr 3, 2012 at 5:03
  • @gideon i have not tried much. I am searching the web right now for info. Commented Apr 3, 2012 at 5:04
  • 3
    Obviously, your XML has XML namespaces (gphoto) defined - you need to check out the XmlNamespaceManager on how to define XML namespace to parse your XML document. Commented Apr 3, 2012 at 5:05

3 Answers 3

3

If you already have an XmlDocument then you can use the function GetElementsByTagName() to create an XmlNodeList that can be accessed similar to an array.

http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx

//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");

//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i=0; i < elemList.Count; i++)
{   
  Console.WriteLine(elemList[i].InnerXml);
} 
Sign up to request clarification or add additional context in comments.

Comments

1

You can select nodes using XPath and SelectSingleNode SelectNodes. Look at http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C for examples. Then you can use for example InnerText to get final. Maybe you need to work with namespaces (gphoto). The //videostatus would select all videostatus elements

Comments

0

You can try using LINQ

        XNamespace ns = XNamespace.Get(""); //use the xmnls namespace here
        XElement element = XElement.Load(""); // xml file path
        var result = element.Descendants(ns + "videostatus")
                     .Select(o =>o.Value).ToList();

       foreach(var values in value)
       {

       }           

Thanks

Deepu

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.