?
Solved

How to create an instance of a XMLDataDocument and insert a node

Posted on 2007-11-15
11
Medium Priority
?
1,221 Views
Last Modified: 2008-09-12
I'm using VB.net 2005

First question:
I am getting an error message "Object reference not set to an instance of an object" when I run the follwing code:
Dim ds1 As New DataSet
ds1.ReadXml("..\RequestedResults1.xml")
        Dim xmlDataDoc As XmlDataDocument
        xmlDataDoc = New XmlDataDocument(ds1)

The error is triggered on the last statement above. I dont know what I'm doing wrong as the dataset 'ds1' shows up fine in my debugger.

2nd Question:
I need to create a node entering in a bank account number. I would like to insert it in front of the 'Time Created' node which is a child element of 'CheckRet'. But since it is a child of child I'm not sure if my syntax below is correct. I'm not sure all of this correct anyway. Any suggestions would be helpful as this is my first time working with XML

Thanks
     
 Dim testnode As XmlElement
        Dim testnode1 As XmlNode
        Dim refnode As XmlNode
        'testnode = xmlDataDoc.CreateNode(XmlNodeType.Element, "TestElement")
        testnode = xmlDataDoc.CreateElement("TestElement")
        testnode1 = xmlDataDoc.CreateTextNode("This is the test")
        testnode.AppendChild(testnode1)
        refnode = xmlDataDoc.ChildNodes("TimeCreated").CloneNode(True)

        xmlDataDoc.InsertBefore(testnode, refnode)






<?xml version="1.0" ?>
<QBXML>
<QBXMLMsgsRs>
<CheckQueryRs requestID="0" statusCode="0" statusSeverity="Info" statusMessage="Status OK">
<CheckRet>
<TxnID>86C6-1194014008</TxnID>
<TimeCreated>2007-11-02T09:33:28-06:00</TimeCreated>
<TimeModified>2007-11-02T09:43:31-06:00</TimeModified>
<EditSequence>1194014611</EditSequence>
<TxnNumber>5680</TxnNumber>
<AccountRef>
<ListID>780001-1182954866</ListID>
<FullName>Cash-Wachovia</FullName>
</AccountRef>
<PayeeEntityRef>
<ListID>1530000-1192115502</ListID>
<FullName>Carrie Wombat_V</FullName>
</PayeeEntityRef>
<RefNumber>11135</RefNumber>
<TxnDate>2007-11-01</TxnDate>
<Amount>3000.00</Amount>
<Memo>Advance</Memo>
<Address>
<Addr1>Carrie Adams</Addr1>
<Addr2>6 Long Circle</Addr2>
<City>Nowheret</City>
<State>TX</State>
<PostalCode>80501</PostalCode>
</Address>
<IsToBePrinted>false</IsToBePrinted>
<ExpenseLineRet>
<TxnLineID>86C8-1194014008</TxnLineID>
<AccountRef>
<ListID>6F0000-1148240632</ListID>
<FullName>Employee Advances</FullName>
</AccountRef>
<Amount>3000.00</Amount>
</ExpenseLineRet>
</CheckRet>
</CheckQueryRs>
</QBXMLMsgsRs>
</QBXML>
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20300810
It is generally accepted to only answer a single question with E-E.  

Everything looks right with the first block of code.  Do you have the full stack trace for the exception?

Bob
0
 

Author Comment

by:Charles_BrandonIndustries
ID: 20300911
How do you do that?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20300931
"How" would depend on what type of application you are working with.

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Charles_BrandonIndustries
ID: 20300958
Sorry,

Im using Visual Basic 2005
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20301020
Not what type of development environment, what type of application (WinForms, ASP.NET web site, etc.)?

Bob
0
 

Author Comment

by:Charles_BrandonIndustries
ID: 20301049
WinForms
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20301510
Add a Try/Catch:

Try
  Dim ds1 As New DataSet
  ds1.ReadXml("..\RequestedResults1.xml")
  Dim xmlDataDoc As XmlDataDocument
  xmlDataDoc = New XmlDataDocument(ds1)
Catch ex As Exception
   Clipboard.SetText(ex.ToString())
End Try

Paste the stack trace into the comment for this question.

Bob
0
 

Author Comment

by:Charles_BrandonIndustries
ID: 20301727
Here is the stack trace

System.NullReferenceException: Object reference not set to an instance of an object.     at System.Xml.XmlDataDocument.OnNestedParentChange(DataRow child, XmlBoundElement childElement, DataColumn childCol)     at System.Xml.XmlDataDocument.OnAddRow(DataRow row)     at System.Xml.XmlDataDocument.LoadTreeFromDataSet(DataSet ds)     at System.Xml.XmlDataDocument.Bind(Boolean fLoadFromDataSet)     at System.Xml.XmlDataDocument.Init(DataSet ds)     at System.Xml.XmlDataDocument..ctor(DataSet dataset)     at ItemQuery.ProcessRequests.ReceiveAndProcess(String stAccountSelected, Boolean binSelectedPayables, DateTime dtStartDate, DateTime dtEndDate, String stBankNumberSelected) in C:\Program Files\Intuit\QBSDK6.0\samples\qbdt\vb\qbfc\itemquery\ItemQuery.NET2\ProcessRequests.vb:line 54
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20312213
It seems to be having a problem with the inferred schema from the XML document.  How many DataTable and DataRelation objects got created with DataSet.ReadXml?

Bob
0
 

Author Comment

by:Charles_BrandonIndustries
ID: 20312481
6 or 7 tables. I don't know how many relations.

Instead of using the method above I ended up just reading the xml file directly into the  object.
        ''''Dim xmlDataDoc As New XmlDataDocument
        ''''xmlDataDoc.Load("..\RequestedResults1.xml")

Still...it would be nice to know what happened because it is obviously very convenient to use.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20312761
That sounds like a) a problem with the XML when read into a DataSet, and b) a bug in the .NET framework.

Here is the code for OnNestedParentChange:

Private Sub OnNestedParentChange(ByVal child As DataRow, ByVal childElement As XmlBoundElement, ByVal childCol As DataColumn)
    Dim rowFromElement As DataRow
    If ((childElement Is MyBase.DocumentElement) OrElse (childElement.ParentNode Is Nothing)) Then
        rowFromElement = Nothing
    Else
        rowFromElement = Me.GetRowFromElement(DirectCast(childElement.ParentNode, XmlElement))
    End If
    Dim nestedParent As DataRow = Me.GetNestedParent(child)
    If (Not rowFromElement Is nestedParent) Then
        If (Not nestedParent Is Nothing) Then
            Me.GetElementFromRow(nestedParent).AppendChild(childElement)
        Else
            Dim nestedParentRelation As DataRelation = XmlDataDocument.GetNestedParentRelation(child)
            If (((childCol Is Nothing) OrElse (nestedParentRelation Is Nothing)) OrElse Convert.IsDBNull(child.Item(childCol))) Then
                Me.EnsureNonRowDocumentElement.AppendChild(childElement)
            Else
                Dim column As DataColumn = Me.FindAssociatedParentColumn(nestedParentRelation, childCol)
                Dim obj2 As Object = column.ConvertValue(child.Item(childCol))
                If ((rowFromElement.tempRecord <> -1) AndAlso (column.CompareValueTo(rowFromElement.tempRecord, obj2) <> 0)) Then
                    Me.EnsureNonRowDocumentElement.AppendChild(childElement)
                End If
            End If
        End If
    End If
End Sub

You will notice that they aren't checking for childElement is Nothing.

Bob
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question