Solved

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

Posted on 2007-11-15
11
1,182 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

9 Experts available now in Live!

Get 1:1 Help Now