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?
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).