Read XML value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • google_groups3@hotmail.com

    #1

    Read XML value

    OK, this is driving me nuts but it must be easier than it seems to me.
    Below is an XML file I have been given. I can write to it fine but I
    just cannot work out the code for reading a value. So, can anyone
    show me how?? I want to read the value "RefreshInterva l"

    <?xml version="1.0" ?>
    - <!-- XML File -->
    - <MyApp>
    - <Configuratio n>
    - <Folders Count="4">
    <F0>c:\Testing\ Upload</Src0>
    <F1>c:\Testing\ Download</Src1>
    <F2>c:\Download s</Src2>
    <F3>c:\Debug</Src3>
    </SourceFolders>
    - <AutoRefresh>
    <RefreshInterva l>15</RefreshInterval >
    </AutoRefresh>
    </Configuration>
    </SafeGuard>

    I have googled my ass off but not found an example that works. So, by
    posting my XML file I am hoping that someone can show me how to read
    that value.

    TIA

    p.s. sorry if this has been asked a million times!
  • Chris Dunaway

    #2
    Re: Read XML value

    On 11 Aug 2004 09:21:42 -0700, google_groups3@ hotmail.com wrote:
    [color=blue]
    > OK, this is driving me nuts but it must be easier than it seems to me.
    > Below is an XML file I have been given. I can write to it fine but I
    > just cannot work out the code for reading a value. So, can anyone
    > show me how?? I want to read the value "RefreshInterva l"
    >
    > <?xml version="1.0" ?>
    > - <!-- XML File -->
    > - <MyApp>
    > - <Configuratio n>
    > - <Folders Count="4">
    > <F0>c:\Testing\ Upload</Src0>
    > <F1>c:\Testing\ Download</Src1>
    > <F2>c:\Download s</Src2>
    > <F3>c:\Debug</Src3>
    > </SourceFolders>
    > - <AutoRefresh>
    > <RefreshInterva l>15</RefreshInterval >
    > </AutoRefresh>
    > </Configuration>
    > </SafeGuard>
    >
    > I have googled my ass off but not found an example that works. So, by
    > posting my XML file I am hoping that someone can show me how to read
    > that value.
    >
    > TIA
    >
    > p.s. sorry if this has been asked a million times![/color]

    Firstly, your XML is not well formed. You have a opening <MyApp> tag, but
    not matching </MyApp> tag. You also have a closing </SafeGuard> tag but no
    opening <SafeGuard> tag.

    Assuming these are just typos, here is some code to read the value of
    RefreshInterval :

    Imports System.Xml

    Dim xDoc As New XmlDocument
    xDoc.Load(xmlfi lenamehere)

    Dim xRefresh As XmlNode
    xRefresh = xDoc.SelectSing leNode("MyApp/AutoRefresh/RefreshInterval ")

    Dim iInterval As Integer = CInt(xRefresh.I nnerText)


    Hope this helps

    --
    Chris

    dunawayc[AT]sbcglobal_lunch meat_[DOT]net

    To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
    replace certain words in my E-Mail address.

    Comment

    • google_groups3@hotmail.com

      #3
      Re: Read XML value

      <SNIP>[color=blue]
      > Firstly, your XML is not well formed. You have a opening <MyApp> tag, but
      > not matching </MyApp> tag. You also have a closing </SafeGuard> tag but no
      > opening <SafeGuard> tag.
      >
      > Assuming these are just typos, here is some code to read the value of
      > RefreshInterval :
      >
      > Imports System.Xml
      >
      > Dim xDoc As New XmlDocument
      > xDoc.Load(xmlfi lenamehere)
      >
      > Dim xRefresh As XmlNode
      > xRefresh = xDoc.SelectSing leNode("MyApp/AutoRefresh/RefreshInterval ")
      >
      > Dim iInterval As Integer = CInt(xRefresh.I nnerText)
      >
      >
      > Hope this helps[/color]

      <SNIP>

      Perfect, Perfect, Perfect!

      p.s. those where just typos. thanks.

      Comment

      • Nick Malik

        #4
        Re: Read XML value

        In addition to just reading a value, and in addition to the comments of
        Chris, I'd suggest you can use the XSD.EXE tool that comes with the .net
        framework sdk. This tool can take a well-formed xml and produce a schema.
        You can edit the schema (graphically) to make sure it is what you want. You
        can then use the same tool to create a class that maps to that schema.

        Then, it's a simple act of deserializing your data into the class. After
        that, you can read EVERY value simply from an object.

        It's pretty sweet.

        --- Nick

        <google_groups3 @hotmail.com> wrote in message
        news:3e3ab30.04 08110821.7b6c39 b5@posting.goog le.com...[color=blue]
        > OK, this is driving me nuts but it must be easier than it seems to me.
        > Below is an XML file I have been given. I can write to it fine but I
        > just cannot work out the code for reading a value. So, can anyone
        > show me how?? I want to read the value "RefreshInterva l"
        >
        > <?xml version="1.0" ?>
        > - <!-- XML File -->
        > - <MyApp>
        > - <Configuratio n>
        > - <Folders Count="4">
        > <F0>c:\Testing\ Upload</Src0>
        > <F1>c:\Testing\ Download</Src1>
        > <F2>c:\Download s</Src2>
        > <F3>c:\Debug</Src3>
        > </SourceFolders>
        > - <AutoRefresh>
        > <RefreshInterva l>15</RefreshInterval >
        > </AutoRefresh>
        > </Configuration>
        > </SafeGuard>
        >
        > I have googled my ass off but not found an example that works. So, by
        > posting my XML file I am hoping that someone can show me how to read
        > that value.
        >
        > TIA
        >
        > p.s. sorry if this has been asked a million times![/color]


        Comment

        Working...