private void ParseValues(DocumentCompositeNode documentNode, XmlToClrImporter.FilteredElement element) { if (element.HasAttributes) { foreach (XmlAttribute xmlAttribute in element.Attributes) { string clrName = this.xmlToClrAdapter.GetClrName(xmlAttribute.LocalName, xmlAttribute.Prefix, xmlAttribute.NamespaceURI); this.CreateLeafNode(documentNode, clrName, xmlAttribute.Value); } } if (!element.HasChildElements) { return; } foreach (XmlElement element1 in element.ChildElements) { XmlToClrImporter.FilteredElement element2 = new XmlToClrImporter.FilteredElement(element1, this.xmlToClrAdapter); if (element2.IsLeaf) { this.CreateLeafNode(documentNode, element2.Name, element2.LeafValue); } else { this.ParseValues(this.CreateCompositeNode(documentNode, element2.Name), element2); } } }
private void ImportFromFile(string xmlFileName) { PerformanceUtility.StartPerformanceSequence(PerformanceEvent.XmlToClrImport); try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(xmlFileName); XmlElement element1 = (XmlElement)Enumerable.FirstOrDefault <XmlNode>(Enumerable.OfType <XmlNode>((IEnumerable)xmlDocument.ChildNodes), (Func <XmlNode, bool>)(node => node is XmlElement)); if (element1 == null || !this.xmlToClrAdapter.ShouldProcessElement(element1.LocalName, element1.Prefix, element1.NamespaceURI)) { throw new InvalidDataException(ExceptionStringTable.EmptyXmlDocument); } XmlToClrImporter.FilteredElement element2 = new XmlToClrImporter.FilteredElement(element1, this.xmlToClrAdapter); this.xmlToClrAdapter.Initialize(element2.Name); this.rootType = this.GetOrCreateCompositeDescription(element2.Name); if (!string.IsNullOrEmpty(this.xmlToClrAdapter.ExpectedRootTypeName) && this.xmlToClrAdapter.ExpectedRootTypeName != this.rootType.Name) { throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.SampleDataXmlReimportWrongRootType, new object[2] { (object)this.rootType.Name, (object)this.xmlToClrAdapter.ExpectedRootTypeName })); } this.ParseType(this.rootType, element2); this.NormalizeTypeDescriptions(); this.CreateClrTypes(); DocumentCompositeNode compositeNode = this.xmlToClrAdapter.CreateCompositeNode(this.rootType.Type); if (compositeNode == null) { return; } this.ParseValues(compositeNode, element2); this.xmlToClrAdapter.Finalize(true); } finally { this.xmlToClrAdapter.Finalize(false); PerformanceUtility.EndPerformanceSequence(PerformanceEvent.XmlToClrImport); } }
private void ParseType(XmlToClrImporter.ClrTypeDescription description, XmlToClrImporter.FilteredElement element) { description.ResetPropertyOccurenceCounts(); if (element.HasAttributes) { foreach (XmlAttribute xmlAttribute in element.Attributes) { string clrName = this.xmlToClrAdapter.GetClrName(xmlAttribute.LocalName, xmlAttribute.Prefix, xmlAttribute.NamespaceURI); this.UpdateAttributeValueType(description, clrName, xmlAttribute.Value); } } if (!element.HasChildElements) { return; } foreach (XmlElement element1 in element.ChildElements) { XmlToClrImporter.FilteredElement element2 = new XmlToClrImporter.FilteredElement(element1, this.xmlToClrAdapter); XmlToClrImporter.ClrPropertyDescription propertyDescription = description[element2.Name]; XmlToClrImporter.ClrTypeDescription existingType = (XmlToClrImporter.ClrTypeDescription)null; if (propertyDescription != null) { propertyDescription.CountOccurence(); existingType = propertyDescription.PropertyType; } XmlToClrImporter.ClrTypeDescription clrTypeDescription; if (element2.IsLeaf) { if (existingType != this.stringTypeDescription) { string leafValue = element2.LeafValue; if (!string.IsNullOrEmpty(leafValue)) { clrTypeDescription = this.GetOrCreateLeafDescription(this.xmlToClrAdapter.InferValueType(leafValue)); } else if (existingType == null) { clrTypeDescription = this.GetOrCreateEmptyDescription(element2.Name); } else { continue; } } else { continue; } } else { clrTypeDescription = this.GetOrCreateCompositeDescription(element2.Name); this.ParseType(clrTypeDescription, element2); } if (propertyDescription == null) { propertyDescription = description[element2.Name]; if (propertyDescription != null) { existingType = propertyDescription.PropertyType; } } XmlToClrImporter.ClrTypeDescription description1 = this.ReconcilePropertyType(existingType, clrTypeDescription); if (propertyDescription == null) { description.AddProperty(element2.Name, description1); } else { propertyDescription.PropertyType = description1; } } }