CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Reading XML metadata

  1. #1
    Join Date
    Oct 2001
    Posts
    80

    Reading XML metadata

    Given the following XML

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <somethinglist>
    - <something>
    <metadata name="ArticleNumber">5555</metadata>
    - </something>
    - </somethinglist>


    My Q: How can i get the value, in this case 5555 and how do i check whether I read it in Articlenumber.

    I tried using XMLTextReader, but the property .LocalName always returns "metadata"!?

    I think should be easy, but I can't find it


    Many thanks in advance...


    Ward

  2. #2
    Join Date
    Jun 2006
    Location
    Minnesota
    Posts
    257

    Re: Reading XML metadata

    If the xml is just a string, do this:

    Code:
    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(someString);
    xDoc.SelectSingleNode("//metadata").InnerText = "5555"
    xDoc.SelectSingleNode("//metadata[@name]").InnerText = "ArticleNumber"
    That will work if "metadata" is just some element of your xml document otherwise I might need more information and do some research.

    edit: noticed that my Attribute XPath was incorrect. Fixed it.
    Last edited by Fishdawg65; July 13th, 2006 at 08:51 AM.

  3. #3
    Join Date
    Jun 2006
    Location
    Minnesota
    Posts
    257

    Re: Reading XML metadata

    I forgot to mention the link for XPath:

    XPath tutorial

    Hope this helps.

  4. #4
    Join Date
    Oct 2001
    Posts
    80

    Re: Reading XML metadata

    Thanks FishDawg65.

    However, I was hoping for a more elegant solution. I haven't tried it yet, so I don't know if it works.

    Anyone else an idea?

  5. #5
    Join Date
    Jun 2006
    Location
    Minnesota
    Posts
    257

    Re: Reading XML metadata

    I'm not sure what you mean by elegant but I was looking XmlTextReader and found this link that explains some of the different methods that you can use:

    XmlTextReader stuff

    There is a method called XmlTextReader.MoveToAttribute(int) Method that will take you to whatever attribute you need. I might suggest looking there.

  6. #6
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Reading XML metadata

    I would suggest a XPath expression too. So you can be sure that the value is coming from the right xml node.
    Useful or not? Rate my posting. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)