Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO.NET convert string to strongly typed XML class

Status
Not open for further replies.

nwruiz

Programmer
Jun 22, 2005
60
US
Hello,

I am retrieving an XML document stored as a string from an Oracle stored procedure (XML was generated through a PL/SQL query):

Code:
<ROWSET>
 <ROW>
  <KY_PND_SEQ_TRANS>100000</KY_PND_SEQ_TRANS>
  <KY_BA_LEAD_ZEROS>000000009</KY_BA_LEAD_ZEROS>
  <DT_PERIOD_START>28-Jun-2007</DT_PERIOD_START>
  <DT_PERIOD_END>30-Jul-2007</DT_PERIOD_END>
  <KY_SUPPLIER>SUPPLIER'S NAME</KY_SUPPLIER>
  <TX_CUST_NAME>CUSTOMER NAME</TX_CUST_NAME>
 </ROW>
</ROWSET>

I am trying to convert this document into a strongly typed class, based on an XSD. The XSD is defined as:

Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="InvoiceSummary" targetNamespace="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"[/URL] xmlns="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"[/URL] xmlns:mstns="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"[/URL] xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
  <xs:element name="ROWSET">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ROW" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="KY_PND_SEQ_TRANS" type="xs:integer" minOccurs="1" maxOccurs="1" />
              <xs:element name="KY_BA_LEAD_ZEROS" type="xs:string" minOccurs="1" maxOccurs="1" />
              <xs:element name="DT_PERIOD_START" type="xs:date" minOccurs="1" maxOccurs="1" />
              <xs:element name="DT_PERIOD_END" type="xs:date" minOccurs="1" maxOccurs="1" />
              <xs:element name="KY_SUPPLIER" type="xs:string" minOccurs="1" maxOccurs="1" />
              <xs:element name="TX_CUST_NAME" type="xs:string" minOccurs="1" maxOccurs="1" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I used xsd.exe to generate a VB.NET partial class for this schema (the class was automatically named RowSet). I am attempting to use the following code to convert the string into my strongly typed XML-based class, but for some reason, it appears that the conversion is null.

Code:
        Dim clob As OracleLob = cmd.Parameters.Item("p_xml_clob").Value

        Const XmlDEC As String = "<?xml version=""1.0"" encoding=""utf-8""?>"

        Dim myXml As String = XmlDEC & ControlChars.CrLf & clob.Value
        myXml = myXml.Replace("<ROWSET>", "<ROWSET xmlns:xsd=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema""[/URL] xmlns:xsi=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance""[/URL] xmlns=""[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"">")[/URL]

        Dim myInvoiceSummary As New Mailer.ROWSET()

        Using myReader As New IO.StringReader(myXml)
            'Console.WriteLine(myReader.ReadToEnd)

            Dim mySer As New Xml.Serialization.XmlSerializer(GetType(ROWSET))

            myInvoiceSummary = DirectCast(mySer.Deserialize(myReader), Mailer.ROWSET)
        End Using

        If myInvoiceSummary.ROW.Length = 0 Then
            ' TODO: Log that no invoice needed to be written.
            Console.WriteLine("No approval requests have been created. Either no transactions require approval or they have already been processed by this application.")
            Return
        End If

ADO.NET seems to think that there are no ROW nodes in my document, even though there is definitely a node in the document. Instead, ROW is Null. Any ideas as to what I might be doing wrong? Thanks for your help.

Nick Ruiz
 
Code:
Imports PPL.PPLSolutions.Billing.BackingSheets

Public Class XmlTest
    Public Sub Main(ByVal args() As String)
        Dim myInvoiceSummary As New Mailer.ROWSET()

        Dim myXml As String = "<?xml version=""1.0"" encoding=""utf-8""?>" & vbNewLine & _
                "    <ROWSET xmlns:xsd=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema""[/URL] xmlns:xsi=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance""[/URL] xmlns=""[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"">"[/URL] & vbNewLine & _
                "         <ROW>" & vbNewLine & _
                "          <KY_PND_SEQ_TRANS>1607458</KY_PND_SEQ_TRANS>" & vbNewLine & _
                "          <KY_BA_LEAD_ZEROS>0382489009</KY_BA_LEAD_ZEROS>" & vbNewLine & _
                "          <DT_PERIOD_START>28-JUN-07</DT_PERIOD_START>" & vbNewLine & _
                "          <DT_PERIOD_END>30-JUL-07</DT_PERIOD_END>" & vbNewLine & _
                "          <KY_SUPPLIER>AESNE</KY_SUPPLIER>" & vbNewLine & _
                "          <TX_CUST_NAME>RAND WHITNEY CONTAINER</TX_CUST_NAME>" & vbNewLine & _
                "         </ROW>" & vbNewLine & _
                "         <ROW>" & vbNewLine & _
                "          <KY_PND_SEQ_TRANS>1607459</KY_PND_SEQ_TRANS>" & vbNewLine & _
                "          <KY_BA_LEAD_ZEROS>0382489210</KY_BA_LEAD_ZEROS>" & vbNewLine & _
                "          <DT_PERIOD_START>28-JUN-07</DT_PERIOD_START>" & vbNewLine & _
                "          <DT_PERIOD_END>30-JUL-07</DT_PERIOD_END>" & vbNewLine & _
                "          <KY_SUPPLIER>AESNE</KY_SUPPLIER>" & vbNewLine & _
                "          <TX_CUST_NAME>RAND WHITNEY CONTAINER</TX_CUST_NAME>" & vbNewLine & _
                "         </ROW>" & vbNewLine & _
                "        </ROWSET>"

        Using myReader As New IO.StringReader(myXml)
            'dim myXmlReader as New Xml.XmlTextReader(myxml, xml.XmlNodeType.Element, new xml.XmlParserContext(
            'Console.WriteLine(myReader.ReadToEnd)

            'Dim adapter As New OracleDataAdapter(cmd)
            'dim myXmlReader as New Xml.XmlTextReader
            Dim mySer As New Xml.Serialization.XmlSerializer(GetType(Mailer.ROWSET))

            myInvoiceSummary = DirectCast(mySer.Deserialize(myReader), Mailer.ROWSET)
        End Using

        Console.WriteLine("ROW count: " & myInvoiceSummary.ROW.Length)
    End Sub
End Class

And below is the class generated by xsd.exe:
Code:
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.832
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd, Version=2.0.50727.42.
'

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd"),[/URL]  _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd",[/URL] IsNullable:=false)>  _
Partial Public Class ROWSET
    
    Private rOWField() As ROWSETROW
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("ROW", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property ROW() As ROWSETROW()
        Get
            Return Me.rOWField
        End Get
        Set
            Me.rOWField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="[URL unfurl="true"]http://tempuri.org/InvoiceSummary.xsd")>[/URL]  _
Partial Public Class ROWSETROW
    
    Private kY_PND_SEQ_TRANSField As String
    
    Private kY_BA_LEAD_ZEROSField As String
    
    Private dT_PERIOD_STARTField As Date
    
    Private dT_PERIOD_ENDField As Date
    
    Private kY_SUPPLIERField As String
    
    Private tX_CUST_NAMEField As String
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType:="integer")>  _
    Public Property KY_PND_SEQ_TRANS() As String
        Get
            Return Me.kY_PND_SEQ_TRANSField
        End Get
        Set
            Me.kY_PND_SEQ_TRANSField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property KY_BA_LEAD_ZEROS() As String
        Get
            Return Me.kY_BA_LEAD_ZEROSField
        End Get
        Set
            Me.kY_BA_LEAD_ZEROSField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType:="date")>  _
    Public Property DT_PERIOD_START() As Date
        Get
            Return Me.dT_PERIOD_STARTField
        End Get
        Set
            Me.dT_PERIOD_STARTField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType:="date")>  _
    Public Property DT_PERIOD_END() As Date
        Get
            Return Me.dT_PERIOD_ENDField
        End Get
        Set
            Me.dT_PERIOD_ENDField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property KY_SUPPLIER() As String
        Get
            Return Me.kY_SUPPLIERField
        End Get
        Set
            Me.kY_SUPPLIERField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property TX_CUST_NAME() As String
        Get
            Return Me.tX_CUST_NAMEField
        End Get
        Set
            Me.tX_CUST_NAMEField = value
        End Set
    End Property
End Class

Nick Ruiz
 
><xs:schema id="InvoiceSummary" targetNamespace=" xmlns=" xmlns:mstns=" xmlns:xs="
On the surface of what I see, try with the targetNamespace and the default namespace settings taken out.

[tt]<xs:schema id="InvoiceSummary" xmlns:mstns=" xmlns:xs="[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top