void unknownNode(object sender, XmlNodeEventArgs e) { int n; string[] parts; string className, nameSpace; if (e.ObjectBeingDeserialized != null) { if (!issueFound) { issueFound = true; } parts = e.ObjectBeingDeserialized.GetType().FullName.Split('.'); if ((n = parts.Length) < 2) { nameSpace = string.Empty; className = parts[0]; } else { nameSpace = string.Join(".", parts, 0, n - 1); className = parts[n - 1]; } addToNamespace(nameSpace, className, e.Name); } else { MyLogger.log(MethodBase.GetCurrentMethod(), "Found " + e.NodeType + " '" + e.Name + "' on " + e.ObjectBeingDeserialized.GetType().FullName); } }
static void handleSchemaInference(XmlSchemaSet v) { CodeCompileUnit ccu; CodeNamespace ns; CodeTypeDeclaration ctd; string key; ccu = new CodeCompileUnit(); ccu.Namespaces.Add(ns = new CodeNamespace()); ns.Imports.AddRange(new CodeNamespaceImport[] { //new CodeNamespaceImport("System.Xml.Schema"), new CodeNamespaceImport("System.Xml.Serialization") }); foreach (XmlSchema xs in v.Schemas()) { ns.Types.Clear(); foreach (XmlSchemaElement xse in xs.Items) { if (xse.ElementSchemaType is XmlSchemaComplexType) { ns.Types.Add(ctd = new CodeTypeDeclaration(key = xse.Name)); MyLogger.log(MethodBase.GetCurrentMethod(), "Adding type: " + key); populateComplexType(ns, ctd, xse.ElementSchemaType as XmlSchemaComplexType); } else { MyLogger.log(MethodBase.GetCurrentMethod(), "unhandled: " + xse.ElementSchemaType); } } outputResult(ccu); } }
internal static void inferSchema(string fileName) { XmlSchemaInference xsi; XmlReaderSettings xrs; //writeElements = false; try { ctrSerialAttr = new CodeTypeReference(writeElements ? "XmlElement" : "XmlAttribute"); xsi = new XmlSchemaInference(); xrs = new XmlReaderSettings(); using (XmlReader xr = XmlReader.Create(fileName, xrs)) { handleSchemaInference(xsi.InferSchema(xr)); } } catch (Exception ex) { MyLogger.log(MethodBase.GetCurrentMethod(), ex); } finally { } }
void addTypeInfo(CodeNamespace nsThis, string className, string fieldName) { CodeTypeDeclaration ctd = null; foreach (CodeTypeDeclaration atype in nsThis.Types) { if (string.Compare(atype.Name, className, true) == 0) { ctd = atype; break; } } if (ctd == null) { MyLogger.log(MethodBase.GetCurrentMethod(), "Adding type: " + className); nsThis.Types.Add(ctd = new CodeTypeDeclaration(className)); ctd.IsPartial = true; ctd.CustomAttributes.Add(new CodeAttributeDeclaration("XmlRoot")); } addField(ctd, fieldName); }
void doDeserialize(string filename, Type t) { XmlDeserializationEvents xde; XmlSerializer xs; XmlReaderSettings xrs; StringBuilder sb; object anObj; ccu = new CodeCompileUnit(); ccu.Namespaces.Add(ns = new CodeNamespace()); ns.Imports.AddRange( new CodeNamespaceImport[] { new CodeNamespaceImport("System.Xml"), new CodeNamespaceImport("System.Xml.Serialization"), }); try { xs = new XmlSerializer(t); xrs = new XmlReaderSettings(); xrs.ValidationEventHandler += Xrs_ValidationEventHandler; xrs.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.AllowXmlAttributes | System.Xml.Schema.XmlSchemaValidationFlags.ReportValidationWarnings; using (XmlReader xr = XmlReader.Create(filename, xrs)) { xde = new XmlDeserializationEvents(); xde.OnUnknownAttribute = this.unknownAttribute; xde.OnUnknownElement = this.unknownElement; xde.OnUnknownNode = this.unknownNode; xde.OnUnreferencedObject = this.unreferencedObject; anObj = xs.Deserialize(xr, xde); MyLogger.log(MethodBase.GetCurrentMethod()); } } catch (Exception ex) { MyLogger.log(MethodBase.GetCurrentMethod(), ex); } finally { if (issueFound) { showResult(sb = new StringBuilder()); } } }
static CodeTypeDeclaration readComplexType(CodeNamespace ns, string className, XmlSchemaComplexType xcdt) { CodeTypeDeclaration ret = null; if (ns.Types.Count > 0) { foreach (CodeTypeDeclaration ctd in ns.Types) { if (string.Compare(ctd.Name, className, true) == 0) { ret = ctd; break; } } } if (ret == null) { MyLogger.log(MethodBase.GetCurrentMethod(), "Adding type: " + className); ns.Types.Add(ret = new CodeTypeDeclaration(className)); ret.IsPartial = true; } populateComplexType(ns, ret, xcdt); return(ret); }
static void populateComplexType(CodeNamespace ns, CodeTypeDeclaration ctd, XmlSchemaComplexType xsct) { XmlSchemaSequence xss; XmlSchemaElement e; CodeMemberField f; CodeTypeDeclaration ctdNew = null; XmlSchemaSimpleType xsst; string key, fname; if (xsct.ContentTypeParticle != null) { if (xsct.ContentTypeParticle is XmlSchemaSequence) { xss = xsct.ContentTypeParticle as XmlSchemaSequence; foreach (var avar in xss.Items) { if (avar is XmlSchemaElement) { e = avar as XmlSchemaElement; if (e.ElementSchemaType is XmlSchemaComplexType) { ctdNew = readComplexType(ns, e.Name, e.ElementSchemaType as XmlSchemaComplexType); fname = e.QualifiedName.Name; ctd.Members.Add( f = new CodeMemberField(ctdNew.Name, "_" + char.ToLower(fname[0]) + fname.Substring(1))); f.Attributes = MemberAttributes.Public; f.CustomAttributes.Add(createAttribute(ctrSerialAttr, fname)); } else if (e.ElementSchemaType is XmlSchemaSimpleType) { xsst = e.ElementSchemaType as XmlSchemaSimpleType; ctdNew = ctd; fname = e.QualifiedName.Name; CodeTypeReference ctr = new CodeTypeReference(typeof(object)); switch (key = xsst.QualifiedName.Name) { case "boolean": ctr = new CodeTypeReference(typeof(bool)); break; case "string": ctr = new CodeTypeReference(typeof(string)); break; default: MyLogger.log(MethodBase.GetCurrentMethod(), "Unhandled: " + key); break; } ctd.Members.Add(f = new CodeMemberField(ctr, "_" + char.ToLower(fname[0]) + fname.Substring(1))); f.Attributes = MemberAttributes.Public; f.CustomAttributes.Add(createAttribute(ctrSerialAttr, fname)); } else { throw new InvalidOperationException("unhandled type:" + e.ElementSchemaType); } } else { MyLogger.log(MethodBase.GetCurrentMethod(), "unhandled: " + avar); } } } else { MyLogger.log(MethodBase.GetCurrentMethod(), "not sequence"); } } }
void unknownAttribute(object sender, XmlAttributeEventArgs e) { MyLogger.log(MethodBase.GetCurrentMethod()); }
void unknownElement(object sender, XmlElementEventArgs e) { MyLogger.log(MethodBase.GetCurrentMethod(), e.ObjectBeingDeserialized.GetType() + ": found " + e.Element.Name); }
void Xrs_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { MyLogger.log(MethodBase.GetCurrentMethod()); }
void unreferencedObject(object sender, UnreferencedObjectEventArgs e) { MyLogger.log(MethodBase.GetCurrentMethod()); }