Saving a XML Document as Resource

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastián Sassi

    #1

    Saving a XML Document as Resource

    Hello,
    I would like to know how can I save an XML Document used to store
    data as an internal file.... something like a resource file, so the user
    can't read or modify it....

    Thanks,
    Sebastián Sassi


  • Marc Scheuner [MVP ADSI]

    #2
    Re: Saving a XML Document as Resource

    > I would like to know how can I save an XML Document used to store[color=blue]
    >data as an internal file.... something like a resource file, so the user
    >can't read or modify it....[/color]

    1) Store the XML to a file on your disk
    2) Add the file to your project
    3) Select that file in your Solution Explorer
    4) Set the "Build Action" to "Embedded Resource"

    To access this later on, do this:

    1) Get the assembly's "Manifest Resource Stream"

    Stream stmXmlData =
    this.GetType(). Assembly.GetMan ifestResourceSt ream("YourNameS pace.YourXmlDoc Name");

    Be careful to get the name of the resource stream right - you have to
    concatenate the name space of your assembly (e.g. "YourNameSpace" ) and
    the name of the XML document in your project (e.g. "YourXmlDoc ") right
    - if you used a file name like "MyDoc.xml" , then you need to specify
    that full name, including the extension!

    2) Read the contents of the XML document from the stream into e.g. a
    string:

    StreamReader oSR = new StreamReader(st mXmlData);
    string sXmlString = oSR.ReadToEnd() ;
    oSR.Close();

    That's it !! Now you have your XML Document as a string (as if you
    loaded it off the disk), and you can use it any way you need.

    Works very well, and with all kinds of resources, really.

    Marc

    Comment

    • Sebastián Sassi

      #3
      Re: Saving a XML Document as Resource

      Thank u very much !!
      Your answer was very useful for me.

      Sebastián Sassi...


      "Marc Scheuner [MVP ADSI]" <m.scheuner@ino va.SPAMBEGONE.c h> escribió en el
      mensaje news:9kc1g0lhg6 ncnjusi0f0t9i0e k7fld8n1r@4ax.c om...[color=blue][color=green]
      > > I would like to know how can I save an XML Document used to store
      > >data as an internal file.... something like a resource file, so the user
      > >can't read or modify it....[/color]
      >
      > 1) Store the XML to a file on your disk
      > 2) Add the file to your project
      > 3) Select that file in your Solution Explorer
      > 4) Set the "Build Action" to "Embedded Resource"
      >
      > To access this later on, do this:
      >
      > 1) Get the assembly's "Manifest Resource Stream"
      >
      > Stream stmXmlData =
      >[/color]
      this.GetType(). Assembly.GetMan ifestResourceSt ream("YourNameS pace.YourXmlDoc N
      ame");[color=blue]
      >
      > Be careful to get the name of the resource stream right - you have to
      > concatenate the name space of your assembly (e.g. "YourNameSpace" ) and
      > the name of the XML document in your project (e.g. "YourXmlDoc ") right
      > - if you used a file name like "MyDoc.xml" , then you need to specify
      > that full name, including the extension!
      >
      > 2) Read the contents of the XML document from the stream into e.g. a
      > string:
      >
      > StreamReader oSR = new StreamReader(st mXmlData);
      > string sXmlString = oSR.ReadToEnd() ;
      > oSR.Close();
      >
      > That's it !! Now you have your XML Document as a string (as if you
      > loaded it off the disk), and you can use it any way you need.
      >
      > Works very well, and with all kinds of resources, really.
      >
      > Marc
      >[/color]


      Comment

      Working...