Solved

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

Posted on 2007-11-15
11
1,187 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
  • 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now