/// <summary> /// opens XSD file and returns file name /// </summary> /// <param name="path">what XSD to open</param> /// <returns>filename if succesfull, otherwise null</returns> public string OpenXSD(string path) { _xsd = new XSDData(path); CurrentNode = _xsd.RootNode; return(_xsd.Name); }
public XSDData(string path) { Path = path; Name = System.IO.Path.GetFileName(path); var content = File.ReadAllText(path, Encoding.UTF8); Content = content; var doc = XDocument.Parse(content); try { StreamReader tr = new StreamReader(Path); var schema = XmlSchema.Read(tr, new ValidationEventHandler(SchemaValidationHandler)); tr.Close(); // compile schema schema.Compile(new ValidationEventHandler(SchemaValidationHandler)); // Create Root Node RootNode = new XSDTreeNode("root", "type", false); DecodeSchema(schema, RootNode); } catch (Exception err) { // TODO handle exception? } }
public void CurrentNodeEnd(string nodeName) { CurrentNodeStatus = NodeStatus.Inside; XSDTreeNode node = null; CurrentNode?.Elements.TryGetValue(nodeName, out node); CurrentNode = node ?? GetFromComplex(CurrentNode, nodeName) ?? CurrentNode; }
public void CurrentNodeOpen(string nodeName) { CurrentNodeStatus = NodeStatus.Open; XSDTreeNode node = null; CurrentNode?.Elements.TryGetValue(nodeName, out node); CurrentNode = node ?? CurrentNode; }
private void DecodeSchema(XmlSchema schema, XSDTreeNode node) { try { DecodeSchema2(schema, node); } catch (Exception err) { } }
private XSDTreeNode GetFromComplex(XSDTreeNode startNode, string nodeName) { XSDTreeNode node = null; var complexElems = _xsd.RootNode.Elements.Where(e => e.Value.ComplexType == true); var type = CurrentNode.Type?.Substring(CurrentNode.Type.LastIndexOf(":") + 1); var complexTypes = complexElems.Where(ce => ce.Key == type); if (complexTypes.Count() > 0) { complexTypes?.First().Value?.Elements?.TryGetValue(nodeName, out node); } node?.UpdateParent(startNode); return(node); }
public void ResetCurrentNode() { CurrentNodeStatus = NodeStatus.Closed; CurrentNode = _xsd.RootNode; }
public void CurrentNodeClose() { CurrentNodeStatus = NodeStatus.Closed; CurrentNode = CurrentNode != _xsd.RootNode ? CurrentNode.Parent ?? CurrentNode : CurrentNode; }
// recursive decoder private XSDTreeNode DecodeSchema2(XmlSchemaObject obj, XSDTreeNode node) { XSDTreeNode newNode = node; // convert the object to all types and then check what type actually exists XmlSchemaAnnotation annot = obj as XmlSchemaAnnotation; XmlSchemaAttribute attrib = obj as XmlSchemaAttribute; XmlSchemaFacet facet = obj as XmlSchemaFacet; XmlSchemaDocumentation doc = obj as XmlSchemaDocumentation; XmlSchemaAppInfo appInfo = obj as XmlSchemaAppInfo; XmlSchemaElement element = obj as XmlSchemaElement; XmlSchemaSimpleType simpleType = obj as XmlSchemaSimpleType; XmlSchemaComplexType complexType = obj as XmlSchemaComplexType; // if annotation, add a tree node and recurse for documentation and app info if (annot != null) { newNode = new XSDTreeNode("--annotation--", "--annotation--", false); node.Add(newNode); foreach (XmlSchemaObject schemaObject in annot.Items) { DecodeSchema2(schemaObject, newNode); } } // if attribute, add an attribute at tree node else if (attrib != null) { node.AddAttribute(attrib.QualifiedName.Name, attrib.SchemaTypeName.Name); } // if facet, add a tree node else if (facet != null) { newNode = new XSDTreeNode(facet.ToString(), facet.ToString(), false); node.Add(newNode); } // if documentation, add a tree node else if (doc != null) { newNode = new XSDTreeNode("--documentation--", "--documentation--", false); node.Add(newNode); } // if app info, add a tree node else if (appInfo != null) { newNode = new XSDTreeNode("--app info--", "--app info--", false); node.Add(newNode); } // if an element, determine whether the element is a simple type or a complex type else if (element != null) { XmlSchemaSimpleType st = element.SchemaType as XmlSchemaSimpleType; XmlSchemaComplexType ct = element.SchemaType as XmlSchemaComplexType; if (st != null) { // this is a simple type element. Recurse. XSDTreeNode node2 = DecodeSchema2(st, newNode); node2.Name = element.Name; node2.Element = true; node2.ComplexType = false; } else if (ct != null) { // this is a complex type element. Recurse. XSDTreeNode node2 = DecodeSchema2(ct, newNode); newNode.Remove(node2); node2.Name = element.Name; node2.Element = true; node2.ComplexType = true; newNode.Add(node2); } else { // This is a plain ol' fashioned element. newNode = new XSDTreeNode(element.QualifiedName.Name, element.ElementSchemaType?.Name ?? element.SchemaTypeName.Name, false); newNode.Element = true; node.Add(newNode); } } // if a simple type, then add a tree node and recurse facets else if (simpleType != null) { newNode = new XSDTreeNode(simpleType.QualifiedName.Name, simpleType.BaseXmlSchemaType.Name, false); node.Add(newNode); XmlSchemaSimpleTypeRestriction rest = simpleType.Content as XmlSchemaSimpleTypeRestriction; if (rest != null) { foreach (XmlSchemaFacet schemaObject in rest.Facets) { DecodeSchema2(schemaObject, newNode); } } } // if a complex type, add a tree node and recurse its sequence else if (complexType != null) { if (null == complexType.Name) { newNode = new XSDTreeNode("--tmp-name--", complexType.BaseXmlSchemaType.Name, true); } else { newNode = new XSDTreeNode(complexType.Name, complexType.BaseXmlSchemaType.Name, true); } node.Add(newNode); XmlSchemaSequence seq = complexType.Particle as XmlSchemaSequence; if (seq != null) { foreach (XmlSchemaObject schemaObject in seq.Items) { DecodeSchema2(schemaObject, newNode); } } } // now recurse any object collection of the type. foreach (PropertyInfo property in obj.GetType().GetProperties()) { if (property.PropertyType.FullName == "System.Xml.Schema.XmlSchemaObjectCollection") { XmlSchemaObjectCollection childObjectCollection = (XmlSchemaObjectCollection)property.GetValue(obj, null); foreach (XmlSchemaObject schemaObject in childObjectCollection) { DecodeSchema2(schemaObject, newNode); } } } return(newNode); }