Tutorial :empty namespace in inline xml literal - how to remove xmlns?



Question:

I am trying to use xml documents to store data for a movie database, but am having an issue, I am new to using xml for documents and have been using tutorials and MSDN to figure it out but I am stuck thanks in advance

here is the code I am using

Imports <"...\movies.xml">    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click        Dim movieToAdd As Movie = getMovieInfo()      MovieBindingSource.AddNew()      Dim doc = XDocument.Load("..\..\movies.xml")      Dim movieAdd = (<temp>                          <Movie>                              <MovieID><%= movieToAdd.MovieID %></MovieID>                              <MovieTitle><%= movieToAdd.MovieTitle %></MovieTitle>                              <Studio><%= movieToAdd.Studio %></Studio>                              <Director><%= movieToAdd.Direcotor %></Director>                              <ReleaseYear><%= movieToAdd.ReleaseYear %></ReleaseYear>                              <Genre><%= movieToAdd.Genre %></Genre>                              <Rating><%= movieToAdd.Rating %></Rating>                              <DVD><%= movieToAdd.DVD %></DVD>                              <BluRay><%= movieToAdd.BluRay %></BluRay>                              <VHS><%= movieToAdd.VHS %></VHS>                              <ScreenFormat><%= movieToAdd.ScreenFormat %></ScreenFormat>                              <MovieCase><%= movieToAdd.MovieCase %></MovieCase>                              <RunTime><%= movieToAdd.RunTime %></RunTime>                              <NumberOfDiscs><%= movieToAdd.NumberOfDiscs %></NumberOfDiscs>                          </Movie>                      </temp>)      Try          Dim addMovie = doc.<movies:Movies>(0)          addMovie.Add(movieAdd.Nodes())          doc.Save("..\..\movies.xml")      Catch ex As Exception          Throw ex      End Try  End Sub    Private Function getMovieInfo() As Movie        Dim movieToAdd As New Movie        movieToAdd.MovieID = CInt(MovieIDTextBox.Text)      movieToAdd.MovieTitle = MovieTitleTextBox.Text      movieToAdd.Studio = StudioTextBox.Text      movieToAdd.Direcotor = DirecotorTextBox.Text      movieToAdd.ReleaseYear = ReleaseYearTextBox.Text      movieToAdd.Genre = GenreComboBox.SelectedText      movieToAdd.Rating = RatingComboBox.SelectedText      movieToAdd.ScreenFormat = ScreenFormatComboBox.SelectedText      movieToAdd.NumberOfDiscs = NumberOfDiscsTextBox.Text      movieToAdd.RunTime = CInt(RunTimeTextBox.Text)      movieToAdd.BluRay = CBool(BluRayCheckBox.CheckState)      movieToAdd.DVD = CBool(DVDCheckBox.CheckState)      movieToAdd.VHS = CBool(VHSCheckBox.CheckState)      movieToAdd.MovieCase = CBool(MovieCaseCheckBox.CheckState)        Return movieToAdd  End Function  

Inn xml file I get this

<Movie xmlns="">    <MovieID>22</MovieID>    <MovieTitle>test</MovieTitle>    <Studio>test</Studio>    <Director>test</Director>    <ReleaseYear>2121</ReleaseYear>    <Genre></Genre>    <Rating></Rating>    <DVD>false</DVD>    <BluRay>false</BluRay>    <VHS>false</VHS>    <ScreenFormat></ScreenFormat>    <MovieCase>false</MovieCase>    <RunTime>123</RunTime>    <NumberOfDiscs>2</NumberOfDiscs>  </Movie>  

Why do I get that xmlns = "" in the movie parent node?

Of I remove the parent node from the movieAdd variable it puts xlmns = in all the nodes

Can anyone show me what I am doing wrong?


Solution:1

It seems (although you didn't share it) that the movies XML file uses namespaces. So I assume that somewhere in that file (probably on the root element) you will have something like xmlns="mymovieurl". The important thing to realize is that each XML element and attribute is identified by a pair of strings. The local name (Movie, DVD, VHS, ...) and the namespace URI (empty, mymovieurl, ...). In your code above, since you didn't specify a default namespace all your elements are in the empty namespace (their namespace URI is an empty string). But your XML file to which you're adding these have its elements in some non-empty namespace (mymovieurl). In order to preserve the namespace for the element you're adding the code has to inject the xmlns="" attribute which marks that element and all its children to be in the empty namespace (just like you specified it in your code).

The solution depends on what you want to achieve. I assume that you want to add the elements into the namespace the rest of the file uses.

One simple way to do that is to add Imports

That means that all elements in your code without a prefix should belong to the namespace "mymovieurl". (Just change that to whatever namespace URI you movies file is using).


Solution:2

The Imports works just like namespace declaration in XML. Since you didn't show us the input XML or the namespace URI you want the output XML to be in, it's impossible to show the right code.

Assuming your file uses namespace URI "mynsuri", then you need to add something like:

Imports <xmlns="mynsuri">  

That will make is such that all elements in your code which don't specify a prefix will belong to the "mynsuri". That is assuming you actually want the Movie element and its children to belong to that namespace.

Maybe a little more explicit way would be to:

Imports <xmlns:movie="mynsuri">  

And then in your code you would create the elements like:

<movie:Movie><movie:MovieID> ...  

The namespace declarations (the xmlns:movie='' attribute) will be added to the output during serialization automatically for you.


Solution:3

k yes I am using a namespace, I do import the namespace, I guess it just did not transfer well in the process, so what I should do in my code is this then?

Imports '<'xmlns:movies="G:\Visual Studio 2008\Projects\Movie Catalog\Movie Catalog\movies.xml">

    Dim movieAdd = (<temp>                          <Movie xmlns="G:\Visual Studio 2008\Projects\Movie Catalog\Movie Catalog\movies.xml">                              <MovieID><%= movieToAdd.MovieID %></MovieID>                              <MovieTitle><%= movieToAdd.MovieTitle %></MovieTitle>                              <Studio><%= movieToAdd.Studio %></Studio>                              <Director><%= movieToAdd.Direcotor %></Director>                              <ReleaseYear><%= movieToAdd.ReleaseYear %></ReleaseYear>                              <Genre><%= movieToAdd.Genre %></Genre>                              <Rating><%= movieToAdd.Rating %></Rating>                              <DVD><%= movieToAdd.DVD %></DVD>                              <BluRay><%= movieToAdd.BluRay %></BluRay>                              <VHS><%= movieToAdd.VHS %></VHS>                              <ScreenFormat><%= movieToAdd.ScreenFormat %></ScreenFormat>                              <MovieCase><%= movieToAdd.MovieCase %></MovieCase>                              <RunTime><%= movieToAdd.RunTime %></RunTime>                              <NumberOfDiscs><%= movieToAdd.NumberOfDiscs %></NumberOfDiscs>                          </Movie>                      </temp>)  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »