Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / General / September 2005

Tip: Looking for answers? Try searching our database.

Embedded XML File

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 22 Sep 2005 17:19 GMT
Hi Guys
Sorry if this post is to the wrong group.
I'm building an app with an embedded XML File. The XML file holds details
which are accessed by the application to perform software installs dependent
on OS type and other criteria listed in the XML File.
I've added the file and set its properties to embedded resource. I'm having
problems referencing it. I have looked through the help and Googles, and
have found out how to do it for images etc.

Private Sub DrawShapesOnForm()
   Dim thisExe As System.Reflection.Assembly
   thisExe = System.Reflection.Assembly.GetExecutingAssembly()
   Dim file As System.IO.Stream = _
       thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg")
   Me.PictureBox1.Image = Image.FromStream(file)
End Sub

When I manipulate this to referencethe xml file I get the folowing error:

Value of type 'System.IO.Stream' cannot be converted to 'String'

This is the code I'm trying to use:
Dim strNameSpace As String =
System.Reflection.Assembly.GetExecutingAssembly.GetName.Name.ToString
Dim str As Stream =
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strNameSpace
+ ".Hotfixes.xml")

Me.TextBox1.Text = str

I think its something to do with the As Stream bit (?) Where am I going
wrong?

Thanks

John
Peter Macej - 23 Sep 2005 08:54 GMT
You are trying to assign Stream object to String variable. You must read
the stream to that variable. This is how I do it:

Dim Assm As System.Reflection.Assembly = Me.GetType().Assembly
' Open the required resource and create stream from it.
Dim xmlStream As System.IO.Stream =
Assm.GetManifestResourceStream("MyAssemblyName" & "MyXmlFile.xml")
Dim reader As New System.IO.StreamReader(xmlStream)
Dim buffer As String = reader.ReadToEnd()
reader.Close()
xmlStream.Close()

The resulting string is in variable "buffer".

Signature

Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
.NET and ASP .NET code

John - 23 Sep 2005 09:50 GMT
Hi Peter.

That was very helpful. Thanks very much for that. Unfortunately, the
variable buffer is a string and I need it returning in a format readable by
an XmlTextReader.
I have included your code as a function, which I assumed would be called
from the code below so that m_xmlr is assigned the returned value from the
function, which would be a variable of type XmlTextReader containing the
contents of the xml resource. How do you assign the resulting string to a
variable of a type readable by an XmlTextReader object, rather than as a
string. If I amend your code so that dim buffer as string becomes dim buffer
as XmlTextReader, I get build errors, "Value of type 'String' cannot be
converted to 'System.Xml.XmlTextReader'"

Here is part of my current code. This successfully reads the XML if
c:\VB.net\HotfixesNet\Hotfixes.xml exists. But on most target computers it
doesn't, hence the wish to embed the xml source in the app.

m_xmlr = New XmlTextReader("C:\vb.net\HotfixesNet\Hotfixes.xml") 'Create the
XML Reader
m_xmlr.WhitespaceHandling = WhitespaceHandling.None 'Disable whitespace so
that you don't have to read over whitespaces

m_xmlr.Read() 'read the xml declaration and advance to Hotfix tag

m_xmlr.Read() 'read the Hotfix tag

etc

etc.

What am I missing here?Thanks very much for your help

Regards,

John

> You are trying to assign Stream object to String variable. You must read
> the stream to that variable. This is how I do it:
[quoted text clipped - 9 lines]
>
> The resulting string is in variable "buffer".
Peter Macej - 23 Sep 2005 10:21 GMT
> variable buffer is a string and I need it returning in a format readable by
> an XmlTextReader.

XmlTextReader has a constructor which accepts Stream argument. So you
can easily create one:

Dim xmlStream As System.IO.Stream =
Assm.GetManifestResourceStream("MyAssemblyName" & "MyXmlFile.xml")

Dim m_xmlr As New XmlTextReader (xmlStream)
m_xmlr.WhitespaceHandling = WhitespaceHandling.None
m_xmlr.Read() 'read the xml declaration and advance to Hotfix tag
m_xmlr.Read() 'read the Hotfix tag
etc
etc.

m_xmlr.Close()
xmlStream.Close()

I haven't tried it but this should work. I parse XML using XmlDocument:
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(bufferAsString)

Signature

Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
.NET and ASP .NET code

John - 23 Sep 2005 15:41 GMT
Hi Peter,

That isn't doing it. When I try putting that on a button, I get "The
operation is not valid due to the current state of the object" as it trys to
run the m_xmlr.WhiteSpaceHandling = WhiltespaceHandling.None
This is what I'm trying to do:
OS = attrOS

m_xmlr = New XmlTextReader("C:\vb.net\HotfixesNet\Hotfixes.xml") 'Create the
XML Reader

m_xmlr.WhitespaceHandling = WhitespaceHandling.None 'Disable whitespace so
that you don't have to read over whitespaces

m_xmlr.Read() 'read the xml declaration and advance to Hotfix tag

m_xmlr.Read() 'read the Hotfix tag

While Not m_xmlr.EOF 'Load the Loop

   m_xmlr.Read()

   If m_xmlr.IsStartElement() Then 'if not start element exit while loop

       m_xmlr.Read()

       OSAttribute = m_xmlr.ReadString

       m_xmlr.Read()

       IDAttribute = m_xmlr.ReadString

       m_xmlr.Read()

       CDPathAttribute = m_xmlr.ReadString

       m_xmlr.Read()

       NetPathAttribute = m_xmlr.ReadString

       m_xmlr.Read()

       SwitchesAttribute = m_xmlr.ReadString

       m_xmlr.Read()

       TypeAttribute = m_xmlr.ReadString

       If OSAttribute = OS And TypeAttribute = "Monthly" Then

           intMonthly = intMonthly + 1

       End If

       If OSAttribute = OS And TypeAttribute = "New" Then

           intNew = intNew + 1

       End If

       If OSAttribute = OS And TypeAttribute = "ITMU" Then

          intITMU = intITMU + 1

       End If

   End If

   StatusBar1.Text = "There are " + CStr(intMonthly) + " Monthly Hotfixes
and " + CStr(intNew) + " New Hotfixes available"

End While

m_xmlr.Close() 'close the reader

End Function

If I comment out the line that is failing, (m_xmlr.WhitespaceHandling =
WhitespaceHandling.None ) the app just seems to hang in debug.

This function counts  the values of elements in the xml and stores values.
It works if the file exists in the path shown
("C:\vb.net\HotfixesNet\Hotfixes.xml")
What I'm trying to achieve is to access (reference) the embedded resourse
xml file instead of the file on the C Drive
I have several buttons which currently do similar things, i.e. access the
xml, read it in and perform actions based on entries in the xml. Each button
currently calls a different function which has broadly similar code in it.
The buttons pass a different attributes to their function.

I suppose it would also be nice to embed the actual call to xml into a
function which returns the contents of the xml file in a format that m_xmlr
can use without having to manipulate the returned data, it would really
simplify matters as I would be able to remove duplicate code from about 3 or
4 functions and replace it with calls the thefunction which returns the xml
file contensts in a format immediately accessible to m_xmlr. However, the
most important thing is to get the functions to reference the embedded
object rather than the one  on the c drive

Sorry to hassle you about this, your help is much appreciated.

Regards,

John

>> variable buffer is a string and I need it returning in a format readable
>> by an XmlTextReader.
[quoted text clipped - 18 lines]
> Dim doc As XmlDocument = New XmlDocument()
> doc.LoadXml(bufferAsString)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.