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 [2010/10/18 09:31] smayr |
swdev:dotnet:using_xml [2011/11/02 16:13] (current) smayr |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | == 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:// | + | 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 === | ||
- | Example: (Source: [[http:// | + | == Using LINQ with XML == |
+ | |||
+ | Example: (Source: [[http:// | ||
<code csharp> | <code csharp> | ||
XDocument xml = XDocument.Load(@" | XDocument xml = XDocument.Load(@" | ||
Line 49: | Line 50: | ||
{ | { | ||
chartListView.Items.Add(new { | chartListView.Items.Add(new { | ||
- | | + | |
- | Type = record.Attribute(" | + | Type = record.Attribute(" |
- | defaultFontName = record.Attribute(" | + | defaultFontName = record.Attribute(" |
- | defaultFontSize = record.Attribute(" | + | defaultFontSize = record.Attribute(" |
- | ID = record.Attribute(" | + | ID = record.Attribute(" |
} | } | ||
); | ); | ||
Line 66: | Line 67: | ||
</ | </ | ||
- | Beware however that myEnumarable is the same object instance as myList, but listAgain is not the same object instance as myEnumerable. Depending on what you want to do and what myEnumerable is, " | + | Beware however that myEnumarable is the same object instance as myList, but listAgain is not the same object instance as myEnumerable. Depending on what you want to do and what myEnumerable is, '' |
- | Chrisw: Oh yes, you're right of course, but the IEnumerable< | + | Chrisw: Oh yes, you're right of course, but the '' |
- | You also could just use the original myList object. (I guess I don't really get the question) – sth Jan 23 '09 at 12:27 | + | You also could just use the original myList object. (I guess I don't really get the question) – sth |
| | ||
It's just that if he edits myList then he would be editing myEnumrable, | It's just that if he edits myList then he would be editing myEnumrable, | ||
- | A List< | + | A '' |
- | The other way around, not every IEnumerable< | + | The other way around, not every '' |
Source: [[http:// | Source: [[http:// | ||
- | == References | + | = Examples |
+ | == Loading and Parsing an XML File into a Data Structure == | ||
+ | |||
+ | For an XML file like this: | ||
+ | <code xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | To format XML content to fit in a data structure like this: | ||
+ | <code csharp> | ||
+ | // | ||
+ | // Class TWorkshop | ||
+ | // | ||
+ | public class TWorkshop | ||
+ | { | ||
+ | public int RecID { get; set; } | ||
+ | public string WorkshopID | ||
+ | public string Name { get; set; } | ||
+ | public TContactInfo VenueInfo | ||
+ | public List< | ||
+ | public List< | ||
+ | public string CancellationPolicy | ||
+ | public string Instructions | ||
+ | public string HotelAccommodations | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | We can implement the logic in a function like this: | ||
+ | <code csharp> | ||
+ | // | ||
+ | /// < | ||
+ | /// Load data from an XML file. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// < | ||
+ | // | ||
+ | public bool LoadFromXmlFile(string AFilename) | ||
+ | { | ||
+ | if (System.IO.File.Exists(AFilename)) | ||
+ | { | ||
+ | xmldoc = XDocument.Load(AFilename); | ||
+ | if (xmldoc != null) | ||
+ | { | ||
+ | // parse xml data and load into TWorkshopList object | ||
+ | List< | ||
+ | xmldata = xmldoc.Root.Elements(" | ||
+ | |||
+ | foreach (XElement el in xmldata) | ||
+ | { | ||
+ | // create TWorkshop record | ||
+ | TWorkshop currec = new TWorkshop(); | ||
+ | |||
+ | // Parse xml record, and add it | ||
+ | currec.VenueInfo.Clear(); | ||
+ | currec.Name | ||
+ | currec.WorkshopID | ||
+ | currec.RecID | ||
+ | currec.VenueInfo.Organization | ||
+ | currec.VenueInfo.Address.AddressLn1 = el.Element(" | ||
+ | currec.VenueInfo.Address.City | ||
+ | currec.VenueInfo.Address.StateProv | ||
+ | currec.VenueInfo.Address.PostalCode = el.Element(" | ||
+ | currec.VenueInfo.PhoneHome | ||
+ | currec.VenueInfo.PhoneWork | ||
+ | currec.VenueInfo.Fax | ||
+ | currec.VenueInfo.Website | ||
+ | currec.CancellationPolicy | ||
+ | currec.Instructions | ||
+ | currec.HotelAccommodations | ||
+ | |||
+ | // Days: extra processing | ||
+ | currec.Days.Clear(); | ||
+ | List< | ||
+ | foreach (XElement elDay in elDays) | ||
+ | { | ||
+ | string DateInInvariantCultureStrFmt = elDay.Value; | ||
+ | DateTime DateLocalized = new DateTime(); | ||
+ | try | ||
+ | { | ||
+ | DateLocalized = DateTime.Parse(DateInInvariantCultureStrFmt, | ||
+ | System.Globalization.CultureInfo.InvariantCulture, | ||
+ | System.Globalization.DateTimeStyles.None); | ||
+ | } | ||
+ | catch(FormatException exc) | ||
+ | { | ||
+ | throw new FormatException(string.Format("' | ||
+ | } | ||
+ | currec.Days.Add(DateLocalized); | ||
+ | } | ||
+ | |||
+ | // PricingDailyPackages: | ||
+ | currec.PricingDailyPackages.Clear(); | ||
+ | List< | ||
+ | foreach (XElement elPkg in elPkgs) | ||
+ | { | ||
+ | TPricingPackage pkg = new TPricingPackage(); | ||
+ | pkg.Name | ||
+ | pkg.Pricing = Convert.ToInt32(elPkg.Element(" | ||
+ | currec.PricingDailyPackages.Add(pkg); | ||
+ | } | ||
+ | |||
+ | // add TWorkshop record | ||
+ | this.Add(currec); | ||
+ | } | ||
+ | |||
+ | // Success | ||
+ | return true; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // Failed to load data from file | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // Failed. File not found. | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == 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:// |