Blank namespace in inline xml literal - how to remove xmlns?

I'm trying to use XML documents to store data for a movie database, but I have a problem, I'm new to using xml for documents and use tutorials and MSDN to figure this out, but I'm stuck in thanks in advance

here is the code i use

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 

Xml file index 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 this xmlns = "" in the parent filter node?

From my parent node from the movieAdd variable movieAdd it puts xlmns = in all nodes

Can someone show me what I'm doing wrong?

+4
source share
3 answers

It seems (although you haven't shared it) that the XML movie file uses namespaces. Therefore, I assume that somewhere in this file (possibly on the root element) you will have something like xmlns = "mymovieurl". It is important to understand that each element and XML attribute is identified by a pair of lines. Local name (Movie, DVD, VHS, ...) and namespace URI (empty, mymovieurl, ...). In your code above, since you did not specify a default namespace, all of your elements are in an empty namespace (their namespace URI is an empty string). But your XML file to which you are adding has its elements in some non-empty namespace (mymovieurl). To preserve the namespace for the element you are adding, you must enter the xmlns = "" attribute, which places this element and all its children in the empty namespace (just like you specified it in your code).

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

One easy way to do this is to add Import

This means that all elements of your code without a prefix must belong to the namespace "mymovieurl". (Just change this to any namespace URI that the movie file uses).

+2
source

Import works just like a namespace declaration in XML. Since you did not tell us the input XML or the namespace URI in which you want the output XML to be included, it is not possible to show the correct code.

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

 Imports <xmlns="mynsuri"> 

This will make it so that all elements of your code that do not specify a prefix will belong to "mynsuri". This assumes that you really want the Movie element and its children to belong to this namespace.

Maybe a slightly more explicit way would be:

 Imports <xmlns:movie="mynsuri"> 

And then in your code you will create elements such as:

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

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

+1
source

k yes I use a namespace, I import a namespace, I think that it just did not pass well in this process, so what should I do in my code, is that then?

Import '<' xmlns: movies = "G: \ Visual Studio 2008 \ Projects \ Movies Catalog \ Movies 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>) 
0
source

All Articles