If I want to work with an xml file as a dataset

I have an XML file and I want to know the easiest way to work with it in general. What is the easiest way to get access to all the data and then have the power to manipulate and modify the data before then saving back to file.

If I have xml file like this:

<people>
<person>
<name>Bob</name>
<age>20</age>
<gender>M</gender>
</person>
...
</people>

and I want to read all about all the people from within VS 2005 in VB.NET 2005, whats the best method

I would like to be able to sort of "transform" it into a class like object and then be able to deal with it like so...

People.Person(0).Age = 25

and that would change "Bob's" age to 25 from 20.

Whats the best way to take that XML file and turn it into an easily manipulatable object.

I figure creating an XSD schema can't hurt and it might have to do with XMLDataDocument.

Can someone explain this to me, how to turn xml into something easy to work with in VS 2005.

Up until now I have been using XMLDocument and working with nodes like Document.SelectSingleNode("...").InnerText....

I know that cannot be the best way to do it. There must be a "strongly typed", "intellisense enabled" method of dealing with xml.

Please help...


Answer this question

If I want to work with an xml file as a dataset

  • arao

    For help on using XmlDocument see the XML Data section in:

    http://samples.gotdotnet.com/quickstart/howto/


  • Lachu

    And to get strong typed classes open your XML up in the XML editor and click the "Create Schema" button, and save the schema to a file. Then from Visual Studio 2005 Command Prompt window use xsd.exe to convert the .xsd to a .vb file. FOr example the following command line:

    xsd /c /namespace:People /language:vb test.xsd

    Produces the following code. You can then serialize the XML off disk into these classes using the System.Xml.Serialization.XmlSerializer class.

    '------------------------------------------------------------------------------
    ' <auto-generated>
    ' This code was generated by a tool.
    ' Runtime Version:2.0.50727.42
    '
    ' Changes to this file may cause incorrect behavior and will be lost if
    ' the code is regenerated.
    ' </auto-generated>
    '------------------------------------------------------------------------------
    Option Strict Off
    Option
    Explicit On
    Imports System.Xml.Serialization
    '
    'This source code was auto-generated by xsd, Version=2.0.50727.42.
    '
    Namespace People
    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
    System.SerializableAttribute(), _
    System.Diagnostics.DebuggerStepThroughAttribute(), _
    System.ComponentModel.DesignerCategoryAttribute("code"), _
    System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
    System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _
    Partial Public Class people
    Private personField() As peoplePerson
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("person")> _
    Public Property person() As peoplePerson()
    Get
    Return Me.personField
    End Get
    Set
    Me.personField = value
    End Set
    End Property
    End Class
    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
    System.SerializableAttribute(), _
    System.Diagnostics.DebuggerStepThroughAttribute(), _
    System.ComponentModel.DesignerCategoryAttribute("code"), _
    System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _
    Partial Public Class peoplePerson
    Private nameField As String
    Private ageField As Byte
    Private genderField As String
    '''<remarks/>
    Public Property name() As String
    Get
    Return Me.nameField
    End Get
    Set
    Me.nameField = value
    End Set
    End Property
    '''<remarks/>
    Public Property age() As Byte
    Get
    Return Me.ageField
    End Get
    Set
    Me.ageField = value
    End Set
    End Property
    '''<remarks/>
    Public Property gender() As String
    Get
    Return Me.genderField
    End Get
    Set
    Me.genderField = value
    End Set
    End Property
    End Class
    End Namespace


  • paksys

    Wow, you guys have been really helpful actually. You weren't kidding when you said XLinq is what I was looking for. I guess, even though I can't use it now in a production environment, it is nice to know it is in the pipeline.

    Thanks also for all your help explaining how to use XML in general. I have decided I prefer the serialized route to using XMLDocument. I think that the strongly typed classes that represent the XML data which can then be serialized back to XML just makes more sense to me. "Unpack" the serialized xml file, work with it, pack it back up and store it to file.

    Unless anyone can point me in the direction of why I would want to use XMLDocument over XMLSerializer Are there any benefits to using the DOM XMLDocument instead of the serializer Performance gains

    I have found that for simple to moderately complex data in medium volumes, I prefer xml to a database for a variety of reasons. Now with the serializer, it makes it even better. The whole weak typed SelectNode("blah") thing was not as intuitive as I would have hoped.

    I am looking forward to XLinq and thanks for all your help.

  • Bart Vercauteren

    This won't help you now, but this is exactly the type of scenario that the forthcoming XLinq library is designed to address. It should allow people with minimal knowledge of XML -- and no desire to learn technologies such as XML schema and XPath -- perform straightforwarding XML manipulation tasks easily and efficiently.

    Sorry for the commercial, but all I can say is "we hear you", and hope to get your feedback as previews of this technology are released. See http://msdn.microsoft.com/netframework/future/linq/


  • D Potter

    Hi,

    In VB.NET 2005, probably the best way to manipulate XML is as you are doing it. Your choices are XMLDocument and XmlParser. XmlParser is a much lower level streaming API. It is appropriate for situations where you are concerned for performance.

    However, if you are not opposed to using beta stuff, and if your project is appropriate for it, check out the XLinq project. See it at http://msdn.microsoft.com/netframework/future/linq/

    Regards,

    Eric White [MSFT]



  • If I want to work with an xml file as a dataset