Results 1 to 11 of 11

Thread: [RESOLVED] [2008] XML advice, please

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Resolved [RESOLVED] [2008] XML advice, please

    Hello,
    I've been given a sample XML file in a structure that I haven't handled in VB.NET before (see image below), and would appreciate some guidance.

    When I read the xml file in with:
    Code:
    Dim ds As DataSet = New DataSet("dataset")
    
    ds.ReadXml("prod2.xml")
    
    With DataGridView1
            .DataSource = ds
            .DataMember = ds.Tables(2).TableName
    End With
    I can see that I've got 3 x tables ... "Interface", "Product", "Value".

    What I'm wondering is how do I get the "Value" table linked to "Product"? I was expecting (rightly or wrongly) to see some inherited values from product displayed in my DataGridView. Looking at the structure of the XML file, it seems to imply that there should be some inheritance???
    Attached Images Attached Images  
    Last edited by penguin5000; Jun 18th, 2008 at 05:39 AM.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2008] XML advice, please

    Can you open your file in notepad, and paste the data between code tags, rather than posting the image?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] XML advice, please

    Thanks for your reply.
    I'm attaching the actual xml file ...
    Attached Files Attached Files
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] XML advice, please

    Value applies to each product. It'd be a property/column of a product, not a separate table. What makes you say it's another table?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] XML advice, please

    When I loop through the DataSet tables to display each TableName, it confirms that it has been broken up into the tables I mentioned, and when I think about the way the company operates, it would make sense for them to provide a history of product value changes for each product (the XML file is a database product update file).
    Last edited by penguin5000; Jun 19th, 2008 at 01:32 AM.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] XML advice, please

    That may unnecessarily complicate things. You're better off using XPathNavigator or XmlDocument to read each product and its corresponding value , price, currency, etc.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] XML advice, please

    Ahhhh - ok, thanks!
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] XML advice, please

    Hi, I'm really struggling with this. Looking at MSDN, it seems to me that the XMLReader is perhaps the best one to use as I don't need to write back to the file.

    The structure of the xml file seems very awkward to work with.

    For example, I can get it to read each attribute of this line:
    Code:
    <Interface CompanyCode="COMP01" CreatedOn="2007-05-27T10:20:00" CreatedBy="PRODUCT" CreatorKey="PROD" SequenceNo="1" CreatorReference="NEWPRODUCT">
    By using the following code:
    Code:
    Dim reader As XmlReader = XmlReader.Create("prod2.xml")
    reader.Read()
    reader.MoveToFirstAttribute()
    For x As Integer = 0 To reader.AttributeCount - 1
        Console.WriteLine(reader.Name & " : " & reader.Item(x).ToString & vbCrLf)
        reader.MoveToNextAttribute()
    Next
    But then it seems that I have to switch to some other way of continuing to read the file because the attributes aren't all along the same line.

    It can't be this difficult, surely?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] XML advice, please

    The XPathNavigator is also a cursor-like model for reading XML which is why I suggested it. You can use the XPathNavigator's Select() method to specify a node or attribute you want. Example:

    navigator.Select("/root/interface/@CreatedBy")

  10. #10
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2008] XML advice, please

    The data set will have relationship object after you read in the xml, then you can grab the child value rows for each product row using the relationship:
    Code:
    Dim ds As New DataSet
    ds.ReadXml("C:\testdata.xml")
    For Each productRow As DataRow In ds.Tables("Product").Rows
        Console.Write(productRow("productcode").ToString & " " & productRow("description").ToString)
        For Each valueRow As DataRow In productRow.GetChildRows("Product_Value")
            Console.Write(" " & valueRow("Price").ToString & " " & valueRow("currency").ToString)
        Next
        Console.WriteLine()
    Next

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] XML advice, please

    Thank you very much for the code snippet. It is really appreciated. I've been battling with this all day, and was getting myself ready for a long night!
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width