Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:using_xml [2011/10/12 10:32] smayr [Using LINQ with XML] |
swdev:dotnet:using_xml [2011/11/02 16:13] (current) smayr |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | == Using XML == | + | = Using XML = |
Editing XML | Editing XML | ||
Line 5: | Line 5: | ||
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:// | 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:// | ||
- | === Parsing XML === | + | == Parsing XML == |
Example (Source: [[http:// | Example (Source: [[http:// | ||
Line 38: | Line 38: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | === Using LINQ with XML === | + | |
+ | == Using LINQ with XML == | ||
Example: (Source: [[http:// | Example: (Source: [[http:// | ||
Line 79: | Line 80: | ||
Source: [[http:// | Source: [[http:// | ||
- | == 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: | ||
Line 272: | Line 273: | ||
</ | </ | ||
- | == References | + | == Example using XmlDocument, |
+ | |||
+ | Sample Data: | ||
+ | <code xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <Param Name=" | ||
+ | <Param Name=" | ||
+ | . . . | ||
+ | </ | ||
+ | < | ||
+ | . . . | ||
+ | </ | ||
+ | < | ||
+ | <Memory Number=" | ||
+ | <Param Name=" | ||
+ | <Param Name=" | ||
+ | . . . | ||
+ | </ | ||
+ | <Memory Number=" | ||
+ | . . . | ||
+ | </ | ||
+ | <Memory Number=" | ||
+ | . . . | ||
+ | </ | ||
+ | <Memory Number=" | ||
+ | . . . | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | . . . | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Code to load Xml document: | ||
+ | <code csharp> | ||
+ | /// | ||
+ | /// < | ||
+ | /// Load data from XML file. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | public void LoadFromXml(string FileName) | ||
+ | { | ||
+ | // using System.Xml; | ||
+ | XmlDocument xmlDoc = new XmlDocument(); | ||
+ | xmlDoc.Load(FileName); | ||
+ | |||
+ | try | ||
+ | { | ||
+ | for (int side = 0; side < 2; side++) | ||
+ | { | ||
+ | // Get elements. | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | XmlNodeList HI = xmlDoc[" | ||
+ | XmlNodeList AutofitEnv = HI[side].ChildNodes[0].ChildNodes; | ||
+ | XmlNodeList ParamConfigLst = HI[side].ChildNodes[1].ChildNodes; | ||
+ | XmlNodeList ParamCustomLst = HI[side].ChildNodes[2].ChildNodes; | ||
+ | XmlNodeList ParamMemoryLst = HI[side].ChildNodes[3].ChildNodes; | ||
+ | |||
+ | // load data into package | ||
+ | LoadXmlNodeListToIntArray(AutofitEnv, | ||
+ | LoadXmlNodeListToDevParamList(ParamConfigLst, | ||
+ | LoadXmlNodeListToDevParamList(ParamCustomLst, | ||
+ | LoadXmlNodeListParamMemToDevParamList(ParamMemoryLst, | ||
+ | } | ||
+ | } | ||
+ | catch (Exception exc) | ||
+ | { | ||
+ | TAppLog.LogException(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// Load Xml node list to Device Parameter list. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private void LoadXmlNodeListToIntArray(XmlNodeList srcParamDataLst, | ||
+ | { | ||
+ | int CurAutofitEnvMemIdx = 0; | ||
+ | for (int i = 0; i < srcParamDataLst.Count; | ||
+ | { | ||
+ | CurAutofitEnvMemIdx | ||
+ | dstIntArray[CurAutofitEnvMemIdx] = Convert.ToInt32(srcParamDataLst[i].Attributes[" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// Load Xml node list to Device Parameter list. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private void LoadXmlNodeListToDevParamList(XmlNodeList srcParamDataLst, | ||
+ | { | ||
+ | for (int i = 0; i < srcParamDataLst.Count; | ||
+ | { | ||
+ | dstParamDataLst.Add(new TDevParam() | ||
+ | { | ||
+ | Name = srcParamDataLst[i].Attributes[" | ||
+ | Value = Convert.ToInt32(srcParamDataLst[i].Attributes[" | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// Load Xml node list to Device Parameter list. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private void LoadXmlNodeListParamMemToDevParamList(XmlNodeList srcParamMemLst, | ||
+ | { | ||
+ | // Format: | ||
+ | // < | ||
+ | // < | ||
+ | // < | ||
+ | // < | ||
+ | // < | ||
+ | // </ | ||
+ | XmlNodeList[] MemParams = new XmlNodeList[srcParamMemLst.Count]; | ||
+ | for (int mem = 0; mem < MemParams.Length; | ||
+ | { | ||
+ | MemParams[mem] = srcParamMemLst.Item(mem).ChildNodes; | ||
+ | |||
+ | for (int paramidx = 0; paramidx < MemParams[mem].Count; | ||
+ | { | ||
+ | dstParamMemLst[mem].Add(new TDevParam() | ||
+ | { | ||
+ | Name = MemParams[mem][paramidx].Attributes[" | ||
+ | Value = Convert.ToInt32(MemParams[mem][paramidx].Attributes[" | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// Get the Xml node list of requested tag name for the given Xml document section. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | /// | ||
+ | private XmlNodeList GetElements(XmlDocument xmlDoc, string SectionName, | ||
+ | { | ||
+ | //XmlNode xmlnodeSection = xmlDoc.GetElementsByTagName(SectionName)[0]; | ||
+ | XmlNode xmlnodeSection = xmlDoc.GetElementsByTagName(SectionName)[0]; | ||
+ | |||
+ | // Convert Section xmlnode into Section xmldoc | ||
+ | // 1. Create empty xmldoc | ||
+ | // 2. Import Section xmlnode into it. | ||
+ | XmlDocument xmldocSection = new XmlDocument(); | ||
+ | xmldocSection.LoadXml("< | ||
+ | XmlNode xn = xmldocSection.ImportNode(xmlnodeSection, | ||
+ | xmldocSection.DocumentElement.AppendChild(xn); | ||
+ | |||
+ | // Search for requested Elements in this Section | ||
+ | return xmldocSection.GetElementsByTagName(ElementTagName); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | = References | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// |