[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1231
  • Last Modified:

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

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
Charles_BrandonIndustries
Asked:
Charles_BrandonIndustries
  • 6
  • 5
1 Solution
 
Bob LearnedCommented:
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
 
Charles_BrandonIndustriesAuthor Commented:
How do you do that?
0
 
Bob LearnedCommented:
"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!

 
Charles_BrandonIndustriesAuthor Commented:
Sorry,

Im using Visual Basic 2005
0
 
Bob LearnedCommented:
Not what type of development environment, what type of application (WinForms, ASP.NET web site, etc.)?

Bob
0
 
Charles_BrandonIndustriesAuthor Commented:
WinForms
0
 
Bob LearnedCommented:
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
 
Charles_BrandonIndustriesAuthor Commented:
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
 
Bob LearnedCommented:
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
 
Charles_BrandonIndustriesAuthor Commented:
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
 
Bob LearnedCommented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now