I want to load the content of an xml file to a DataTable. I created a Class named News that contains the following function.
Public Shared Function listOfEmployees() As DataTable
Dim dt As New DataTable
Dim xmlPath As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/News.xml")
Dim xmlSchema As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/schemas/News.xsd")
dt = New DataTable()
dt.ReadXmlSchema(xmlSchema)
dt.ReadXml(xmlPath)
Return dt
End Function
And I get the following Error:
DataTable does not support schema inference from Xml.
I want to call the the function from an Object Data Source and connected to a GridView.
My XML file looks as follow:
<?xml version="1.0" encoding="utf-8" ?>
<news>
<newsItem>
<id>01</id>
<visible>true</visible>
<title>Season of Open Doors</title>
<date>2010-10-16T13:00:00</date>
<content>Season of Open Doors at 1:00PM. Venue: 1642 Union Blvd, Allentown PA. 18109. Tel: (484) 201-4262</content>
<imageUrl>invite.jpg</imageUrl>
<imageAltText>RCCG Strong Tower 1st Anniversary</imageAltText>
</newsItem>
<newsItem>
<id>02</id>
<visible>true</visible>
<title>Anniversary Celebration</title>
<date>2010-10-17T12:00:00</date>
<content>RCCG Strong Tower 1st Anniversary Celebration at 12:00PM. Venue: 1642 Union Blvd, Allentown PA. 18109. Tel: (484) 201-4262</content>
<imageUrl>news2.jpg</imageUrl>
<imageAltText>Anniversary Celebration</imageAltText>
</newsItem>
</news>
Try taking out xmlns="http://tempuri.org/News.xsd" from the schema, it does not belong there, unless you want to define elements in a certain namespace but your sample XML does not use namespaces.
Martin Honnen --- MVP Data Platform Development
My blog
Thank you Martin for replying. I ended up re-evaluating and taking a different approach:
Public Shared Function newsDS() As DataSet
Dim ds As New DataSet
Dim xmlPath As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/News.xml")
Dim xmlSchema As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/schemas/News.xsd")
ds = New DataSet()
ds.ReadXml(xmlPath)
Return ds
End Function
Public Shared Function newsDT() As DataTable
Dim dt As New DataTable
Dim ds As New DataSet
ds = newsDS()
dt = ds.Tables(0)
Return dt
End Function
'Select the top 3 found
Public Shared Function newsDV(ByVal rowCount As Integer) As DataView
Dim dt As New DataTable
dt = newsDT()
Dim dtn As DataTable = dt.Clone()
For i As Integer = 0 To rowCount - 1
dtn.ImportRow(dt.Rows(i))
Next
Dim dv As DataView
dv = New DataView(dtn)
Return dv
End Function
Marked as answer by diazmayo on Sep 29, 2010 03:05 PM
diazmayo
Member
69 Points
147 Posts
DataTable does not support schema inference from Xml.
Sep 28, 2010 06:51 PM|LINK
I want to load the content of an xml file to a DataTable. I created a Class named News that contains the following function.
Public Shared Function listOfEmployees() As DataTable
Dim dt As New DataTable
Dim xmlPath As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/News.xml")
Dim xmlSchema As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/schemas/News.xsd")
dt = New DataTable()
dt.ReadXmlSchema(xmlSchema)
dt.ReadXml(xmlPath)
Return dt
End Function
And I get the following Error:
DataTable does not support schema inference from Xml.
I want to call the the function from an Object Data Source and connected to a GridView.
My XML file looks as follow:
<?xml version="1.0" encoding="utf-8" ?>
<news>
<newsItem>
<id>01</id>
<visible>true</visible>
<title>Season of Open Doors</title>
<date>2010-10-16T13:00:00</date>
<content>Season of Open Doors at 1:00PM. Venue: 1642 Union Blvd, Allentown PA. 18109. Tel: (484) 201-4262</content>
<imageUrl>invite.jpg</imageUrl>
<imageAltText>RCCG Strong Tower 1st Anniversary</imageAltText>
</newsItem>
<newsItem>
<id>02</id>
<visible>true</visible>
<title>Anniversary Celebration</title>
<date>2010-10-17T12:00:00</date>
<content>RCCG Strong Tower 1st Anniversary Celebration at 12:00PM. Venue: 1642 Union Blvd, Allentown PA. 18109. Tel: (484) 201-4262</content>
<imageUrl>news2.jpg</imageUrl>
<imageAltText>Anniversary Celebration</imageAltText>
</newsItem>
</news>
My Schema Looks as follow:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="News" elementFormDefault="qualified" xmlns="http://tempuri.org/News.xsd" xmlns:mstns="http://tempuri.org/News.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="news">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="32" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="visible" type="xs:boolean" />
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="256" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="date" type="xs:dateTime" />
<xs:element name="content">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="4000" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="imageUrl">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="256" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="imageAltText">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="256" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
diazmayo
Member
69 Points
147 Posts
Re: DataTable does not support schema inference from Xml.
Sep 29, 2010 01:58 PM|LINK
Anyone? x.X
Martin_Honne...
Star
14481 Points
2006 Posts
MVP
Re: DataTable does not support schema inference from Xml.
Sep 29, 2010 02:57 PM|LINK
Try taking out xmlns="http://tempuri.org/News.xsd" from the schema, it does not belong there, unless you want to define elements in a certain namespace but your sample XML does not use namespaces.
My blog
diazmayo
Member
69 Points
147 Posts
Re: DataTable does not support schema inference from Xml.
Sep 29, 2010 03:04 PM|LINK
Thank you Martin for replying. I ended up re-evaluating and taking a different approach:
Public Shared Function newsDS() As DataSet
Dim ds As New DataSet
Dim xmlPath As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/News.xml")
Dim xmlSchema As String = System.Web.HttpContext.Current.Request.MapPath("~/App_Data/schemas/News.xsd")
ds = New DataSet()
ds.ReadXml(xmlPath)
Return ds
End Function
Public Shared Function newsDT() As DataTable
Dim dt As New DataTable
Dim ds As New DataSet
ds = newsDS()
dt = ds.Tables(0)
Return dt
End Function
'Select the top 3 found
Public Shared Function newsDV(ByVal rowCount As Integer) As DataView
Dim dt As New DataTable
dt = newsDT()
Dim dtn As DataTable = dt.Clone()
For i As Integer = 0 To rowCount - 1
dtn.ImportRow(dt.Rows(i))
Next
Dim dv As DataView
dv = New DataView(dtn)
Return dv
End Function