Home All Groups Group Topic Archive Search About
Author
23 Feb 2006 9:38 PM
Ben Schumacher
Using an XmlDataSource to populate the GridView control, I can't set the
DataField property of my BoundField controls to read actual xml node values.
It only works if the DataField reads an xml node attribute.  This is
ridiculous.  How can I read actual node values?

I have a GridView control as follows ...

<asp:GridView ID="GridView1" DataSourceID="XmlDataSource1" runat="Server">
    <Columns>
        <asp:BoundField DataField="ProjectNbr" />
    </Columns>
</asp:GridView>

Here is the XmlDataSource control ...

<asp:XmlDataSource ID="XmlDataSourceControl1"
DataFile="~/documents/checkforresults.xml"
XPath="DocumentPacket/Folders/Folder" runat="Server" />

And here is the xml structure ...

<DocumentPacket>
    <Folders>
        <Folder>
            <ProjectNbr>12345</ProjectNbr>
            <ProjectDesc>Project Description 12345</ProjectDesc>
        </Folder>
        <Folder>
            <ProjectNbr>54321</ProjectNbr>
            <ProjectDesc>Project Description 54321</ProjectDesc>
        </Folder>
    </Folders>
</DocumentPacket>

I keep getting an error "A field or property with the name 'Project Nbr' was
not found on the selected data source.".  If make the ProjectNbr and
ProjectDesc values as attributes in the Folder node like <Folder
ProjectNbr="12345" ProjectDesc="Project Description 12345"> everything works
fine.  Why does the DataField property of the BoundField control have to be
an attribute in the xml node?  Can't the DataField read an actual xml node
value??????

Author
24 Feb 2006 7:20 AM
Phillip Williams
Hi Ben,

You probably would have to use the XPath data binding expression within
ItemTemplates, e.g.

<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                Project #</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server"
Text='<%#XPath("ProjectNbr") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Description</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server"
Text='<%#XPath("ProjectDesc") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Show quoteHide quote
"Ben Schumacher" wrote:

> Using an XmlDataSource to populate the GridView control, I can't set the
> DataField property of my BoundField controls to read actual xml node values.
> It only works if the DataField reads an xml node attribute.  This is
> ridiculous.  How can I read actual node values?
>
> I have a GridView control as follows ...
>
> <asp:GridView ID="GridView1" DataSourceID="XmlDataSource1" runat="Server">
>     <Columns>
>         <asp:BoundField DataField="ProjectNbr" />
>     </Columns>
> </asp:GridView>
>
> Here is the XmlDataSource control ...
>
> <asp:XmlDataSource ID="XmlDataSourceControl1"
> DataFile="~/documents/checkforresults.xml"
> XPath="DocumentPacket/Folders/Folder" runat="Server" />
>
> And here is the xml structure ...
>
> <DocumentPacket>
>     <Folders>
>         <Folder>
>             <ProjectNbr>12345</ProjectNbr>
>             <ProjectDesc>Project Description 12345</ProjectDesc>
>         </Folder>
>         <Folder>
>             <ProjectNbr>54321</ProjectNbr>
>             <ProjectDesc>Project Description 54321</ProjectDesc>
>         </Folder>
>     </Folders>
> </DocumentPacket>
>
> I keep getting an error "A field or property with the name 'Project Nbr' was
> not found on the selected data source.".  If make the ProjectNbr and
> ProjectDesc values as attributes in the Folder node like <Folder
> ProjectNbr="12345" ProjectDesc="Project Description 12345"> everything works
> fine.  Why does the DataField property of the BoundField control have to be
> an attribute in the xml node?  Can't the DataField read an actual xml node
> value??????
>
>
>
Author
24 Feb 2006 2:32 PM
Ben Schumacher
Thanks!  I'll give it a try.  Why would MS create that server control
(BoundField) to only read xml node attributes?

Show quoteHide quote
"Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message
news:CC3585E9-F17D-40D8-A8E5-808270158D51@microsoft.com...
> Hi Ben,
>
> You probably would have to use the XPath data binding expression within
> ItemTemplates, e.g.
>
> <asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
> AutoGenerateColumns="false">
>    <Columns>
>        <asp:TemplateField>
>            <HeaderTemplate>
>                Project #</HeaderTemplate>
>            <ItemTemplate>
>                <asp:Label ID="Label2" runat="server"
> Text='<%#XPath("ProjectNbr") %>'></asp:Label>
>            </ItemTemplate>
>        </asp:TemplateField>
>        <asp:TemplateField>
>            <HeaderTemplate>
>                Description</HeaderTemplate>
>            <ItemTemplate>
>                <asp:Label ID="Label1" runat="server"
> Text='<%#XPath("ProjectDesc") %>'></asp:Label>
>            </ItemTemplate>
>        </asp:TemplateField>
>    </Columns>
> </asp:GridView>
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Ben Schumacher" wrote:
>
>> Using an XmlDataSource to populate the GridView control, I can't set the
>> DataField property of my BoundField controls to read actual xml node
>> values.
>> It only works if the DataField reads an xml node attribute.  This is
>> ridiculous.  How can I read actual node values?
>>
>> I have a GridView control as follows ...
>>
>> <asp:GridView ID="GridView1" DataSourceID="XmlDataSource1"
>> runat="Server">
>>     <Columns>
>>         <asp:BoundField DataField="ProjectNbr" />
>>     </Columns>
>> </asp:GridView>
>>
>> Here is the XmlDataSource control ...
>>
>> <asp:XmlDataSource ID="XmlDataSourceControl1"
>> DataFile="~/documents/checkforresults.xml"
>> XPath="DocumentPacket/Folders/Folder" runat="Server" />
>>
>> And here is the xml structure ...
>>
>> <DocumentPacket>
>>     <Folders>
>>         <Folder>
>>             <ProjectNbr>12345</ProjectNbr>
>>             <ProjectDesc>Project Description 12345</ProjectDesc>
>>         </Folder>
>>         <Folder>
>>             <ProjectNbr>54321</ProjectNbr>
>>             <ProjectDesc>Project Description 54321</ProjectDesc>
>>         </Folder>
>>     </Folders>
>> </DocumentPacket>
>>
>> I keep getting an error "A field or property with the name 'Project Nbr'
>> was
>> not found on the selected data source.".  If make the ProjectNbr and
>> ProjectDesc values as attributes in the Folder node like <Folder
>> ProjectNbr="12345" ProjectDesc="Project Description 12345"> everything
>> works
>> fine.  Why does the DataField property of the BoundField control have to
>> be
>> an attribute in the xml node?  Can't the DataField read an actual xml
>> node
>> value??????
>>
>>
>>
Author
16 Mar 2006 4:31 PM
Scott Kelley
I ran into just this in the past couple of days.  Microsoft has their way of
doing it and they are pro attributes for some reason (Check out Sql Server
For XML Explicit).  Anyway, the way around this for me was to use an XSL
transformation to create attributes out of nodes.  For example.

Original XML

<?xml version="1.0" standalone="yes"?>
<applications>
  <application>
    <applicationkey>XXX</applicationkey>
    <applicationname>My Application</applicationname>
    <url>www.shawonline1.com</url>
    <owner>The Boxx</owner>
    <status>1</status>
  </application>
</applications>

XSL Transformation

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt">
   <xsl:strip-space elements="*" />
   <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"
standalone="yes" />
   <xsl:template match="/">
      <xsl:for-each select="applications">
         <xsl:element name="applications">
            <xsl:for-each select="application">
               <xsl:element name="application">
                  <xsl:attribute name="applicationkey">
                     <xsl:value-of select="applicationkey" />
                  </xsl:attribute>

                  <xsl:attribute name="applicationname">
                     <xsl:value-of select="applicationname" />
                  </xsl:attribute>

                  <xsl:attribute name="url">
                     <xsl:value-of select="url" />
                  </xsl:attribute>

                  <xsl:attribute name="owner">
                     <xsl:value-of select="owner" />
                  </xsl:attribute>

                  <xsl:attribute name="status">
                     <xsl:value-of select="status" />
                  </xsl:attribute>
               </xsl:element>
            </xsl:for-each>
         </xsl:element>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

Now for the code.

        XmlDataSource objXmlDS = new XmlDataSource();
        objXmlDS.Data = strAppsXml; // Here is where you put the xsl in.
        objXmlDS.TransformFile = "~/RemapApplications.xsl"; // This is the
xsl file name.

        gvApplications.DataSource = objXmlDS;
        gvApplications.DataBind();

This gave me the results I was looking for.  Hope it helps.

Scott.


Show quoteHide quote
"Ben Schumacher" <bschumac***@navegate.com> wrote in message
news:%23$fpy8UOGHA.648@TK2MSFTNGP14.phx.gbl...
> Thanks!  I'll give it a try.  Why would MS create that server control
> (BoundField) to only read xml node attributes?
>
> "Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message
> news:CC3585E9-F17D-40D8-A8E5-808270158D51@microsoft.com...
> > Hi Ben,
> >
> > You probably would have to use the XPath data binding expression within
> > ItemTemplates, e.g.
> >
> > <asp:GridView ID="GridView1" runat="server"
DataSourceID="XmlDataSource1"
> > AutoGenerateColumns="false">
> >    <Columns>
> >        <asp:TemplateField>
> >            <HeaderTemplate>
> >                Project #</HeaderTemplate>
> >            <ItemTemplate>
> >                <asp:Label ID="Label2" runat="server"
> > Text='<%#XPath("ProjectNbr") %>'></asp:Label>
> >            </ItemTemplate>
> >        </asp:TemplateField>
> >        <asp:TemplateField>
> >            <HeaderTemplate>
> >                Description</HeaderTemplate>
> >            <ItemTemplate>
> >                <asp:Label ID="Label1" runat="server"
> > Text='<%#XPath("ProjectDesc") %>'></asp:Label>
> >            </ItemTemplate>
> >        </asp:TemplateField>
> >    </Columns>
> > </asp:GridView>
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Ben Schumacher" wrote:
> >
> >> Using an XmlDataSource to populate the GridView control, I can't set
the
> >> DataField property of my BoundField controls to read actual xml node
> >> values.
> >> It only works if the DataField reads an xml node attribute.  This is
> >> ridiculous.  How can I read actual node values?
> >>
> >> I have a GridView control as follows ...
> >>
> >> <asp:GridView ID="GridView1" DataSourceID="XmlDataSource1"
> >> runat="Server">
> >>     <Columns>
> >>         <asp:BoundField DataField="ProjectNbr" />
> >>     </Columns>
> >> </asp:GridView>
> >>
> >> Here is the XmlDataSource control ...
> >>
> >> <asp:XmlDataSource ID="XmlDataSourceControl1"
> >> DataFile="~/documents/checkforresults.xml"
> >> XPath="DocumentPacket/Folders/Folder" runat="Server" />
> >>
> >> And here is the xml structure ...
> >>
> >> <DocumentPacket>
> >>     <Folders>
> >>         <Folder>
> >>             <ProjectNbr>12345</ProjectNbr>
> >>             <ProjectDesc>Project Description 12345</ProjectDesc>
> >>         </Folder>
> >>         <Folder>
> >>             <ProjectNbr>54321</ProjectNbr>
> >>             <ProjectDesc>Project Description 54321</ProjectDesc>
> >>         </Folder>
> >>     </Folders>
> >> </DocumentPacket>
> >>
> >> I keep getting an error "A field or property with the name 'Project
Nbr'
> >> was
> >> not found on the selected data source.".  If make the ProjectNbr and
> >> ProjectDesc values as attributes in the Folder node like <Folder
> >> ProjectNbr="12345" ProjectDesc="Project Description 12345"> everything
> >> works
> >> fine.  Why does the DataField property of the BoundField control have
to
> >> be
> >> an attribute in the xml node?  Can't the DataField read an actual xml
> >> node
> >> value??????
> >>
> >>
> >>
>
>