Solved

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

Posted on 2007-11-15
11
1,214 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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