Author Topic: Data binding with XML linq  (Read 2780 times)

0 Members and 1 Guest are viewing this topic.

djee

  • Newt
  • Posts: 49
Data binding with XML linq
« on: August 31, 2015, 04:42:49 PM »
I have spent literally hours searching for how to do this and unfortunatly being a vb.net novice cannot work out how to do this. I am trying to grab the data I have stored in XML and bind it to a DataGridTextColumn. Anybody has some example I could get my teeth in or some suggestion on how to accomplish this?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Data binding with XML linq
« Reply #1 on: August 31, 2015, 05:28:52 PM »
there is no easy way to do this, xml can have a very flat structure or be very nested and a datagrid is basically a flat data table.

So, depending on the structure, if it's pretty flat you may be able to use something like xpath to query some nodes and build a dataset object and bind that to the datagrid. Another way would be to use xpath and manually load the grid by hand, sounds like work but you only have to write it once but it does hard code for that particular xml schema.

Speaking of a schema, you could use xsd.exe to write classes for the xml objects, this can enable you to bind the objects to property grids say and perhaps the datagrid. This way you can query the xml objects like any other class in .net. You will need to create the schema first but xsd.exe can do that for you too.

F# has TypeProviders that do pretty much the same thing as xsd.exe but on the fly, very cool stuff there! I'd imagine you may be able to use these from vb.net with a bit of study.

Do you have a more concrete example of what you need to do?
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Data binding with XML linq
« Reply #2 on: August 31, 2015, 05:37:55 PM »
Seems like I did this a little while back and pretty I used
XMLDataProvider
https://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider(v=vs.110).aspx

I will see if I can dig it up

djee

  • Newt
  • Posts: 49
Re: Data binding with XML linq
« Reply #3 on: August 31, 2015, 08:25:36 PM »
Do you have a more concrete example of what you need to do?
I'm building this apps so that it set some option path according to selected projet (for my office). I would like to populate my DataGridTextColumn (or any other relevant UI element) with some info stored in an xml file. This file contains misc value like projet number, manager, units, etc... I've already writen the part were i write and read from xml (using some LINQ query).
I'm new to databinding and xaml... was hoping to get more guidance here on how to accomplish this...
Still reading about Dynamic Properties in LINQ to XML Classes...
Maybe i'm going to far? How do you usually store information for persistent usage? in a .txt file?

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Data binding with XML linq
« Reply #4 on: September 01, 2015, 07:53:40 AM »
Seems like I did this a little while back and pretty I used
XMLDataProvider
https://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider(v=vs.110).aspx

I will see if I can dig it up

Or you can use Serialization.  If you build a [Serializable] class then its this easy.
Code - C#: [Select]
  1. var myserialclass= new MySerialClass ();
  2. var streamReader = new StreamReader("c\\mydirectory\\myfile.xml");
  3. var textReader = new XmlTextReader(streamReader);
  4. var serializer = new XmlSerializer(myserialclass.GetType());
  5.  
  6. if (serializer.CanDeserialize(textReader))
  7. {
  8.     var obj = serializer.Deserialize(textReader);
  9.     var type = myserialclass.GetType();
  10.     var props = type.GetProperties();
  11.      foreach (var prop in props)
  12.      {
  13.         prop.SetValue(myserialclass, prop.GetValue(obj, null), null);
  14.       }
  15. }
  16.  
  17. textReader.Close();
  18. streamReader.Close();
Revit 2019, AMEP 2019 64bit Win 10

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Data binding with XML linq
« Reply #5 on: September 01, 2015, 10:37:08 AM »
Maybe i'm going to far? How do you usually store information for persistent usage? in a .txt file?

Depends on the scale and use.  I frequently use XML for "jagged" storage where a regular table (ie. database or CSV file) would make life difficult.  A lot comes down to what is going to be stored and how its going to be used - that will dictate the type and organization of the storage method.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Data binding with XML linq
« Reply #6 on: September 01, 2015, 11:01:04 AM »
How important is it for 2 people in office to have same values?
Is it the end of the world if information is lost to reset to default?

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Data binding with XML linq
« Reply #7 on: September 01, 2015, 03:20:44 PM »
I use the classes at the link below to read/write settings to xml, registry, or ini files.  It is well put together and works great for what it is trying to do.


It is not a general xml, registry, ini file reader/writer.  Its sole purpose is to read/write settings to either xml, registry, or ini.


http://www.codeproject.com/Articles/5304/Read-Write-XML-files-Config-files-INI-files-or-the
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

djee

  • Newt
  • Posts: 49
Re: Data binding with XML linq
« Reply #8 on: September 01, 2015, 10:18:47 PM »
Or you can use Serialization.
I did not know about serialization/Deserialization... Seems like its exactly what i'm looking for... Now i'll have to find how to databind my property to my xaml code... (never stops...) Found the following for binary serialization (over SoapSerializer --> cant edit file & smaller filesize) (when defining classes you have to specify <Serializable()> at the beginning...);
I've attach a small project example showing both serialization type in action below... VB.NET of course...  :-D
Code: [Select]
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class BinarySerializer

    Shared Sub SerializeToFile(ByVal FileInfo As FileInfo, ByVal [Object] As Object)

        Dim locFs As FileStream = New FileStream(FileInfo.FullName, FileMode.Create)
        Dim locBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
        locBinaryFormatter.Serialize(locFs, [Object])
        locFs.Flush()
        locFs.Close()

    End Sub

    Shared Function DeserializeFromFile(ByVal FileInfo As FileInfo) As Object

        Dim locObject As Object

        Dim locFs As FileStream = New FileStream(FileInfo.FullName, FileMode.Open)
        Dim locBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.File))
        locObject = locBinaryFormatter.Deserialize(locFs)
        locFs.Close()
        Return locObject

    End Function

End Class

« Last Edit: September 01, 2015, 10:28:31 PM by djee »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Data binding with XML linq
« Reply #9 on: September 02, 2015, 07:43:25 AM »
about serialization/Deserialization... Seems like its exactly what i'm looking for... Now i'll have to find how to databind my property to my xaml code... (never stops...) Found the following for binary serialization (over SoapSerializer --> cant edit file & smaller filesize) (when defining classes you have to specify <Serializable()> at the beginning...);
I've attach a small project example showing both serialization type in action below... VB.NET of course...  :-D

Binding is the easy part.  Your [Serializable] class will have an ObservableCollection<MyTabularData> to represent the tabular data.  Bind the DataGrid's ItemsSource to that collection and the DataGridTextColumn's will be bound to the individual properties of the class MyTabularData.  Make sure MyTablularData inherits INotifyPropertyChanged and put change notification in each property.  The DataGrid will handle the rest.

Now if you really want to do this the right way.  Then I would wrap my model, MyTabularData, in a viewmodel, let the viewmodel handle the PropertyChanges, and bind the viewmodel to the DataGrid.  Either way will work just depend on how much code you want to write.
Revit 2019, AMEP 2019 64bit Win 10

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Data binding with XML linq
« Reply #10 on: September 22, 2015, 01:28:58 PM »
This is a little off topic but I just posted an XML file reader program called XML QuickView on my site.
It isn't fancy but shows data in grid view, schema text and raw text. I find it be a helpful tool. Maybe others will too.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions