DataGrid, sort numeric column after parsing xml

  • Thread starter Thread starter Anthony
  • Start date Start date
A

Anthony

I parsed a xml file to a DataGrid.
Now i'd like to sort a column with numeric values.

I figured out that everything is sorted as a string value.
How can i sort this column as a numeric value.
Found some examples with Google, but nothing works for me.

Anthony.
 
Anthony said:
I parsed a xml file to a DataGrid.
Now i'd like to sort a column with numeric values.

I figured out that everything is sorted as a string value.
How can i sort this column as a numeric value.

Are you using a DataSet and ReadXml()?
The right approach is to define an XML schema that defines the right
numeric type you want, then use ReadXmlSchema() on the DataSet before
you load the XML instance document with the ReadXml() method.
 
Hi,

This is likely because they are still strings. Not sure how you parse the
file but it would be best to expose each column under its expected type
(dates, integers, booleans or whatever). Here it seems to me that you just
expose all values as strings...

How do you bind this XML file to your datagrid ?
 
This is likely because they are still strings. Not sure how you parse the
file but it would be best to expose each column under its expected type
(dates, integers, booleans or whatever). Here it seems to me that you just
expose all values as strings...

You should have a "XML To Schema" item in VS that will infer a schema from
your XML file. Then you'll be able to tweak the schema if needed and to use
this schema when loading the XML file so that its data are exposed with the
type they should have...
 
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(@"d:\myxmlfile.xml");
DataSet ds = new DataSet("books");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "title";

Anthony
 
Anthony said:
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(@"d:\myxmlfile.xml");
DataSet ds = new DataSet("books");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "title";

You don't need the XmlDataDocument but you need an XML schema for your
XML document. Visual Studio can generate one for you, of course you
might need to edit schema types to what you want. Then use e.g.
DataSet ds = new DataSet();
ds.ReadXmlSchema("schema.xsd");
ds.ReadXml("file.xml");
 
You don't need the XmlDataDocument but you need an XML schema for your XML
document. Visual Studio can generate one for you, of course you might need
to edit schema types to what you want. Then use e.g.
DataSet ds = new DataSet();
ds.ReadXmlSchema("schema.xsd");
ds.ReadXml("file.xml");

Works exelent !
What's the best place to save the schema?
If possible i'd like to integrate it with my project.
(i need to parse a xml file from another application. So normally there's no
schema available)

Anthony
 
You don't need the XmlDataDocument but you need an XML schema for your XML
document. Visual Studio can generate one for you, of course you might need
to edit schema types to what you want. Then use e.g.
DataSet ds = new DataSet();
ds.ReadXmlSchema("schema.xsd");
ds.ReadXml("file.xml");

Is it possible to use Linq in the 'same' way ?
(I need a query for some elements)

Anthony
 
Anthony said:
Is it possible to use Linq in the 'same' way ?
(I need a query for some elements)

What kind of LINQ do you want to do? There is LINQ to DataSet that
allows you querying your DataSet. There is LINQ to XML that allows
querying an XML document but for that you need to load the XML document
into a System.Xml.Linq.XDocument (or XElement) and there is no
connection between a DataSet and an XDocument.
 
What kind of LINQ do you want to do? There is LINQ to DataSet that allows
you querying your DataSet. There is LINQ to XML that allows querying an
XML document but for that you need to load the XML document into a
System.Xml.Linq.XDocument (or XElement) and there is no connection between
a DataSet and an XDocument.

Don't know what's the best solution. (no experience so far)
I need to make a query (xml file or DataSet) and add the final results to my
DataGrid.
Maybe you can give me some advice ?

Did you read my previous quetion about the schema ?

Anthony
 
Anthony said:
What's the best place to save the schema?
If possible i'd like to integrate it with my project.
(i need to parse a xml file from another application. So normally there's no
schema available)

If you want to integrate the schema with your application then you could
embed it as a resource in your assembly. You would then need to load it with
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcestream.aspx
and pass that stream to the ReadXmlSchema method.
 
What kind of LINQ do you want to do? There is LINQ to DataSet that allows
you querying your DataSet. There is LINQ to XML that allows querying an
XML document but for that you need to load the XML document into a
System.Xml.Linq.XDocument (or XElement) and there is no connection between
a DataSet and an XDocument.

More specifically:
Is there a way to populate a DataSet via a LINQ query?
 
Back
Top