Home Forums WPF controls Xceed DataGrid for WPF DataGrid Binding XML with LINQ

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #27089 |

    Hello,

    I wondered if somebody could write an example for Binding XML to a DataGrid with LINQ.

    I’m new to all this stuff and I would really appreciate this. Please, if someone does this I’d like to have the whole Project not just code snippets, I think you will test it anyway and have a project then.

    I would be deeply grateful.

     

    ebber
     

    Imported from legacy forums. Posted by ebber (had 5811 views)

    Xceed Support
    Member
    Post count: 5658

    The “Binding to a LINQ query (XML) example in the documentation should help you get started:

    <Grid xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid“>
      <Grid.Resources>

        <xcdg:DataGridCollectionViewSource x:Key=“cvs_orders”
                        
    Source=“{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},
                                         
    Path=XmlData}“/>

      </Grid.Resources>

     

      <xcdg:DataGridControl x:Name=“OrdersGrid”
                           
    ItemsSource=“{Binding Source={StaticResource cvs_orders}}”/>

    </Grid>

     CODE_BEHIND

    public IEnumerable XmlData
    {
     get
     {
       XDocument document = App.NorthwindDocument;

       var data = from order in document.Element( “dataroot” ).Descendants( “Orders” )
                  select
    new
                  {
                    
    ShipCountry = order.Element( “ShipCountry” ).Value,
                    ShipCity = order.Element(
    “ShipCity” ).Value,
                    ShipAddress = order.Element(
    “ShipAddress” ).Value,
                    ShipName = order.Element(
    “ShipName” ).Value,
                    ShipVia = order.Element(
    “ShipVia” ).Value,
                    Freight = order.Element(
    “Freight” ).Value
                  };
       
    return data.ToList();
     }
    }

     

    Imported from legacy forums. Posted by Jenny [Xceed] (had 3832 views)

    User (Old forums)
    Member
    Post count: 23064

     Hi,

     
    I thought something like this would happen. Now I have no idea where to put this lower part of the code. May you help me in this next small step?

     

    ebber
     

    Imported from legacy forums. Posted by ebber (had 1151 views)

    Xceed Support
    Member
    Post count: 5658

    The Source property of the DataGridCollectionView is bound to the XmlData property, which according to the binding is located in the parent Window:

    Source=“{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=XmlData}“/>

    That said, the XmlData property should be placed in your Window that corresponds to the XAML code-behind page. If you are using a Page (or any other container), then  you can simply change the binding accordingly.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1024 views)

    User (Old forums)
    Member
    Post count: 23064

    Now, I don’t know more than before. Maybe today is not my day.

     

    I’ll tell you again what I want. Maybe then it all will become clear, to me or to you.

    I don’t know where to place this code :     public IEnumerable XmlData……

    If I put it in the code file that goes with my window I get this error: 

     Error    1    The type or namespace name ‘IEnumerable’ could not be found (are you missing a using directive or an assembly reference?)

    Would you help me again?

     

    ebber

     

    Imported from legacy forums. Posted by ebber (had 802 views)

    Xceed Support
    Member
    Post count: 5658

    Add a using directive to : System.Collections  (using System.Collections;)

    or use the complete qualified name for IEnumerable : System.Collections.IEnumerable XmlData…

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1027 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks. This works now.

    But of course there is a new problem:   XDocument document = App.NorthwindDocument;

    What is a XDocument? I read in the Help but there was nothing useful. I assume it is some kind of XML Document and the problem is small like the last one.

    I made this so you can say immediately that it is wrong, so I won’t have to ask again:

     <?xml version=”1.0″ encoding=”utf-8″ ?>
    <dataroot>
      <Orders ShipCountry=”a” ShipCity=”a” ShipAddress=”a” ShipName=”a” ShipVia=”a” Freight=”a” />
      <Orders ShipCountry=”b” ShipCity=”b” ShipAddress=”b” ShipName=”b” ShipVia=”b” Freight=”b” />
    </dataroot>
     

    Imported from legacy forums. Posted by ebber (had 876 views)

    Xceed Support
    Member
    Post count: 5658

    Hi ebber,

        These questions are more relevant to getting started with Linq to XML rather than the Xceed DataGrid for WPF.

        I suggest going to the MSDN Linq to XML web-site and forum if you need further assistance getting started with Linq to XML.

       Linq to Xml Web Site

    Imported from legacy forums. Posted by Marcus [Xceed] (had 730 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,

     I think you are right. I’m sorry.

     ebber
     

    ps: I found my problem, it is very misterious. The System.Xml.Linq-Namespace is missing, which contains the XDocument. I know this is the wrong place to ask this but if someone has a clue he may write me a pm.

     

    Imported from legacy forums. Posted by ebber (had 921 views)

    Xceed Support
    Member
    Post count: 5658

    You need to add the LINQ assembly to your references: System.Xml.Linq.dll

     

    Imported from legacy forums. Posted by Jenny [Xceed] (had 681 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks, it’s working now, not all but this one.

    I can’t find a mistake but it doesn’t want to load the data. I made new coloums with the filednames ShipCountry….

    Either my XML file is wrong :

     <?xml version=”1.0″ encoding=”utf-8″ ?>
    <dataroot>
      <Orders ShipCountry=”a” ShipCity=”a” ShipAddress=”a” ShipName=”a” ShipVia=”a” Freight=”a” />
      <Orders ShipCountry=”b” ShipCity=”b” ShipAddress=”b” ShipName=”b” ShipVia=”b” Freight=”b” />
    </dataroot>

    or maybe I have to do something with the Grid first? 

    Imported from legacy forums. Posted by ebber (had 1010 views)

    User (Old forums)
    Member
    Post count: 23064

    [quote user=”Jenny”]

    The Source property of the DataGridCollectionView is bound to the XmlData property, which according to the binding is located in the parent Window:

    Source=“{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=XmlData}“/>

    That said, the XmlData property should be placed in your Window that corresponds to the XAML code-behind page. If you are using a Page (or any other container), then  you can simply change the binding accordingly.

    [/quote]

     Hi There

    I am working on the same example however VS2008 keeps telling me that in order to use AncestorType I have to have a Mode.  I have added the Mode=FindAncestor, in front of the AncestorType attrib but now VS2008 is telling me I need to define the AncestorType, but its right there???

     

    Anyone got any suggestions as to why this snippet of code is throwing these errors up in VS 2008?

     

    Cheers

    Paul 

    Imported from legacy forums. Posted by Paul (had 966 views)

    User (Old forums)
    Member
    Post count: 23064

    Jenny,

    How can do the same, returning a class?

       var data = from order in document.Element( “dataroot” ).Descendants( “Orders” )
                  select
    new SomeClass
                  {
                    
    ShipCountry = order.Element( “ShipCountry” ).Value,
                    ShipCity = order.Element(
    “ShipCity” ).Value,
                    ShipAddress = order.Element(
    “ShipAddress” ).Value,
                    ShipName = order.Element(
    “ShipName” ).Value,
                    ShipVia = order.Element(
    “ShipVia” ).Value,
                    Freight = order.Element(
    “Freight” ).Value
                  };

     

    Thanks,

    Henrique

    Imported from legacy forums. Posted by Henrique (had 7078 views)

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.