Both sides previous revision
Previous revision
|
|
swdev:dotnet:using_xml [2011/11/02 16:12] smayr [Example using XmlDocument, XmlElement, XmlNodeList] |
swdev:dotnet:using_xml [2011/11/02 16:13] (current) smayr |
== Using XML == | = Using XML = |
| |
Editing XML | Editing XML |
Normally, binding a ListView to a datasource (the xml) and let WPF databinding handle the update of the XML data automatically. To do this you could create a [[http://msdn.microsoft.com/en-us/library/84sxtbxh(VS.80).aspx|DataSet from your XML]] and then bind that using ListView.ItemsSource. You would then create a DataTemplate to define the visual representation of a record in your xml. This could be input controls that'd allow you do directly edit the record within your listview. If you prefer a master-detail style view you would bing the detail view to the current item of your listview (e.g. UserControl.DataContext={Binding CurrentItem, ElementName=myListView}). The rest will be handled by WPF. UPDATE: Here is an [[http://msdn.microsoft.com/en-us/library/cc165615.aspx|example]] how you could even bind directly to your XDocument, so so you do not necessarily have to use DataSets. (Source: [[http://stackoverflow.com/questions/1659734/wpf-and-c-gridview-row-selection-and-xml-datasource-how-to-make-the-connecti|bitbonk on StackOverflow.com]]) | Normally, binding a ListView to a datasource (the xml) and let WPF databinding handle the update of the XML data automatically. To do this you could create a [[http://msdn.microsoft.com/en-us/library/84sxtbxh(VS.80).aspx|DataSet from your XML]] and then bind that using ListView.ItemsSource. You would then create a DataTemplate to define the visual representation of a record in your xml. This could be input controls that'd allow you do directly edit the record within your listview. If you prefer a master-detail style view you would bing the detail view to the current item of your listview (e.g. UserControl.DataContext={Binding CurrentItem, ElementName=myListView}). The rest will be handled by WPF. UPDATE: Here is an [[http://msdn.microsoft.com/en-us/library/cc165615.aspx|example]] how you could even bind directly to your XDocument, so so you do not necessarily have to use DataSets. (Source: [[http://stackoverflow.com/questions/1659734/wpf-and-c-gridview-row-selection-and-xml-datasource-how-to-make-the-connecti|bitbonk on StackOverflow.com]]) |
| |
=== Parsing XML === | == Parsing XML == |
| |
Example (Source: [[http://stackoverflow.com/questions/55828/best-practices-to-parse-xml-files|Lukas Šalkauskas]]): | Example (Source: [[http://stackoverflow.com/questions/55828/best-practices-to-parse-xml-files|Lukas Šalkauskas]]): |
* [[http://msdn.microsoft.com/en-us/library/bb387098.aspx|LINQ to XML]] | * [[http://msdn.microsoft.com/en-us/library/bb387098.aspx|LINQ to XML]] |
* [[http://msdn.microsoft.com/en-us/library/182eeyhh.aspx|Intro to XML Serialization]] | * [[http://msdn.microsoft.com/en-us/library/182eeyhh.aspx|Intro to XML Serialization]] |
=== Using LINQ with XML === | |
| == Using LINQ with XML == |
| |
Example: (Source: [[http://stackoverflow.com/questions/1659734/wpf-and-c-gridview-row-selection-and-xml-datasource-how-to-make-the-connecti|StackOverflow.com]]) | Example: (Source: [[http://stackoverflow.com/questions/1659734/wpf-and-c-gridview-row-selection-and-xml-datasource-how-to-make-the-connecti|StackOverflow.com]]) |
Source: [[http://stackoverflow.com/questions/472669/c-freely-convert-between-listt-and-ienumerablet|David B, Frederik Gheysels]] | Source: [[http://stackoverflow.com/questions/472669/c-freely-convert-between-listt-and-ienumerablet|David B, Frederik Gheysels]] |
| |
== Examples == | = Examples = |
=== Loading and Parsing an XML File into a Data Structure === | == Loading and Parsing an XML File into a Data Structure == |
| |
For an XML file like this: | For an XML file like this: |
== Example using XmlDocument, XmlElement, XmlNodeList == | == Example using XmlDocument, XmlElement, XmlNodeList == |
| |
| Sample Data: |
| <code xml> |
| <FittingSession> |
| <Instruments> |
| <Instrument Name="Cue" ProductCode="PROD_CUE" EarSide="0" MaxMemories="5"> |
| <AutofitEnvironments> |
| <AutofitEnvironment Name="Undefined" Index="0" Memory="0" /> |
| <AutofitEnvironment Name="Undefined" Index="0" Memory="1" /> |
| <AutofitEnvironment Name="Undefined" Index="0" Memory="2" /> |
| <AutofitEnvironment Name="Undefined" Index="0" Memory="3" /> |
| </AutofitEnvironments> |
| <ParametersConfig> |
| <Param Name="number_of_programs" Value="0" /> |
| <Param Name="program_switch_mode" Value="0" /> |
| . . . |
| </ParametersConfig> |
| <ParametersCustom> |
| . . . |
| </ParametersCustom> |
| <ParametersMemory> |
| <Memory Number="0"> |
| <Param Name="Input_mux" Value="0" /> |
| <Param Name="Preamp_gain0" Value="4" /> |
| . . . |
| </Memory> |
| <Memory Number="1"> |
| . . . |
| </Memory> |
| <Memory Number="2"> |
| . . . |
| </Memory> |
| <Memory Number="3"> |
| . . . |
| </Memory> |
| </ParametersMemory> |
| </Instrument> |
| |
| <Instrument Name="Cue" ProductCode="PROD_CUE" EarSide="1" MaxMemories="5"> |
| . . . |
| </Instrument> |
| |
| </Instruments> |
| </FittingSession> |
| </code> |
| |
| Code to load Xml document: |
<code csharp> | <code csharp> |
///---------------------------------------------------------------------------------------- | ///---------------------------------------------------------------------------------------- |
</code> | </code> |
| |
Sample Data: | = References = |
<code xml> | |
<FittingSession> | |
<Instruments> | |
<Instrument Name="Cue" ProductCode="PROD_CUE" EarSide="0" MaxMemories="5"> | |
<AutofitEnvironments> | |
<AutofitEnvironment Name="Undefined" Index="0" Memory="0" /> | |
<AutofitEnvironment Name="Undefined" Index="0" Memory="1" /> | |
<AutofitEnvironment Name="Undefined" Index="0" Memory="2" /> | |
<AutofitEnvironment Name="Undefined" Index="0" Memory="3" /> | |
</AutofitEnvironments> | |
<ParametersConfig> | |
<Param Name="number_of_programs" Value="0" /> | |
<Param Name="program_switch_mode" Value="0" /> | |
. . . | |
</ParametersConfig> | |
<ParametersCustom> | |
. . . | |
</ParametersCustom> | |
<ParametersMemory> | |
<Memory Number="0"> | |
<Param Name="Input_mux" Value="0" /> | |
<Param Name="Preamp_gain0" Value="4" /> | |
. . . | |
</Memory> | |
<Memory Number="1"> | |
. . . | |
</Memory> | |
<Memory Number="2"> | |
. . . | |
</Memory> | |
<Memory Number="3"> | |
. . . | |
</Memory> | |
</ParametersMemory> | |
</Instrument> | |
| |
<Instrument Name="Cue" ProductCode="PROD_CUE" EarSide="1" MaxMemories="5"> | |
. . . | |
</Instrument> | |
| |
</Instruments> | |
</FittingSession> | |
</code> | |
| |
== References == | |
* [[http://msdn.microsoft.com/en-us/library/cc165615.aspx|MSDN: How To: Bind to XDocument, XElement, or LINQ for XML Query Results]] | * [[http://msdn.microsoft.com/en-us/library/cc165615.aspx|MSDN: How To: Bind to XDocument, XElement, or LINQ for XML Query Results]] |
* [[http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx|MSDN: XElement Class]] | * [[http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx|MSDN: XElement Class]] |