Working with an Embedded XML File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay Douglas

    #1

    Working with an Embedded XML File

    Hello all,
    I have a basic XML file of which I set the Build Action to "Embedded
    Resource" The XML file is simple:

    <root>
    <item>
    <name>Test Name</name>
    <description>Te st Description</description>
    </item>
    </root>

    I'm having a heck of a time reading the xml file embedded in the assembly.
    I'm more than certain that I'm missing some sort of name space element in
    the XML.. I've searched high and low and I am unable to find any sample
    code that works. I get errors such as invalid object, etc.

    I was hoping that somebody might be able to refer me to a url that explains
    how to create the xml file correctly and the code to read the embedded xml
    file.

    C# and vb.net examples are welcome.

    Thanks in advance.

    Jay



  • Klaus H. Probst

    #2
    Re: Working with an Embedded XML File

    If you've compiled the XML file as an embedded resource then you can do
    something like this:


    using System;
    using System.IO;
    using System.Reflecti on;
    using System.Xml;

    class Application
    {
    static void Main(string[] args) {

    Stream s =
    Assembly.GetExe cutingAssembly( ).GetManifestRe sourceStream("C SharpConsole.XM L
    File1.xml");

    XmlDocument xdoc = new XmlDocument();

    StreamReader reader = new StreamReader(s) ;

    xdoc.LoadXml(re ader.ReadToEnd( ));

    reader.Close();

    }
    }

    or:

    Imports System.Xml
    Imports System.Reflecti on
    Imports System.IO

    Module Main

    Sub Main()

    Dim s As Stream =
    Assembly.GetExe cutingAssembly( ).GetManifestRe sourceStream("X MLFile1.xml")

    Dim xdoc As New XmlDocument

    Dim reader As New StreamReader(s)

    xdoc.LoadXml(re ader.ReadToEnd( ))

    reader.Close()

    End Sub

    End Module

    Notice in C# you have to prepend the project name (this is not a namespace)
    to the file name. IIRC this is due to the way the VB compiler stores
    resources in the assembly.

    --
    Klaus H. Probst, MVP
    http://www.vbbox.com/


    "Jay Douglas" <REMOVEIFNOTSPA M_jay@squarei.c om> wrote in message
    news:%23wuPUg$J EHA.2884@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hello all,
    > I have a basic XML file of which I set the Build Action to "Embedded
    > Resource" The XML file is simple:
    >
    > <root>
    > <item>
    > <name>Test Name</name>
    > <description>Te st Description</description>
    > </item>
    > </root>
    >
    > I'm having a heck of a time reading the xml file embedded in the assembly.
    > I'm more than certain that I'm missing some sort of name space element in
    > the XML.. I've searched high and low and I am unable to find any sample
    > code that works. I get errors such as invalid object, etc.
    >
    > I was hoping that somebody might be able to refer me to a url that[/color]
    explains[color=blue]
    > how to create the xml file correctly and the code to read the embedded xml
    > file.
    >
    > C# and vb.net examples are welcome.
    >
    > Thanks in advance.
    >
    > Jay
    >
    >
    >[/color]


    Comment

    • Jay Douglas

      #3
      Re: Working with an Embedded XML File

      Perfect.

      Thanks a ton.


      "Klaus H. Probst" <usenet001@vbbo x.com> wrote in message
      news:eg8OyRAKEH A.3728@TK2MSFTN GP12.phx.gbl...[color=blue]
      > If you've compiled the XML file as an embedded resource then you can do
      > something like this:
      >
      >
      > using System;
      > using System.IO;
      > using System.Reflecti on;
      > using System.Xml;
      >
      > class Application
      > {
      > static void Main(string[] args) {
      >
      > Stream s =
      >[/color]
      Assembly.GetExe cutingAssembly( ).GetManifestRe sourceStream("C SharpConsole.XM L[color=blue]
      > File1.xml");
      >
      > XmlDocument xdoc = new XmlDocument();
      >
      > StreamReader reader = new StreamReader(s) ;
      >
      > xdoc.LoadXml(re ader.ReadToEnd( ));
      >
      > reader.Close();
      >
      > }
      > }
      >
      > or:
      >
      > Imports System.Xml
      > Imports System.Reflecti on
      > Imports System.IO
      >
      > Module Main
      >
      > Sub Main()
      >
      > Dim s As Stream =
      > Assembly.GetExe cutingAssembly( ).GetManifestRe sourceStream("X MLFile1.xml")
      >
      > Dim xdoc As New XmlDocument
      >
      > Dim reader As New StreamReader(s)
      >
      > xdoc.LoadXml(re ader.ReadToEnd( ))
      >
      > reader.Close()
      >
      > End Sub
      >
      > End Module
      >
      > Notice in C# you have to prepend the project name (this is not a[/color]
      namespace)[color=blue]
      > to the file name. IIRC this is due to the way the VB compiler stores
      > resources in the assembly.
      >
      > --
      > Klaus H. Probst, MVP
      > http://www.vbbox.com/
      >
      >
      > "Jay Douglas" <REMOVEIFNOTSPA M_jay@squarei.c om> wrote in message
      > news:%23wuPUg$J EHA.2884@TK2MSF TNGP12.phx.gbl. ..[color=green]
      > > Hello all,
      > > I have a basic XML file of which I set the Build Action to "Embedded
      > > Resource" The XML file is simple:
      > >
      > > <root>
      > > <item>
      > > <name>Test Name</name>
      > > <description>Te st Description</description>
      > > </item>
      > > </root>
      > >
      > > I'm having a heck of a time reading the xml file embedded in the[/color][/color]
      assembly.[color=blue][color=green]
      > > I'm more than certain that I'm missing some sort of name space element[/color][/color]
      in[color=blue][color=green]
      > > the XML.. I've searched high and low and I am unable to find any sample
      > > code that works. I get errors such as invalid object, etc.
      > >
      > > I was hoping that somebody might be able to refer me to a url that[/color]
      > explains[color=green]
      > > how to create the xml file correctly and the code to read the embedded[/color][/color]
      xml[color=blue][color=green]
      > > file.
      > >
      > > C# and vb.net examples are welcome.
      > >
      > > Thanks in advance.
      > >
      > > Jay
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...