使用c#将xsd转换为xml

时间:2023-02-08 21:47:04

How can I generate xml from xsd without xsd.exe?

如何在没有xsd.exe的情况下从xsd生成xml?

2 个解决方案

#1


12  

I think I googled it for you. Use XmlSampleGenerator from MSDN

我想我用Google搜索了它。使用MSDN中的XmlSampleGenerator

Sample use:

样品用途:

XmlTextWriter textWriter = new XmlTextWriter("po.xml", null);
textWriter.Formatting    = Formatting.Indented;
XmlQualifiedName qname   = new XmlQualifiedName("PurchaseOrder",       
                           "http://tempuri.org");
XmlSampleGenerator generator = new XmlSampleGenerator("po.xsd", qname);
genr.WriteXml(textWriter);

#2


3  

The problem has solved.

问题已经解决了。

private void CreateXML(XmlNode xsdNode, XmlElement element, ref XmlDocument xml)
    {
        if (xsdNode.HasChildNodes)
        {
            var childs = xsdNode.ChildNodes;
            foreach (XmlNode node in childs)
            {
                XmlElement newElement = null;
                if (node.Name == "xs:element")
                {
                    newElement = xml.CreateElement(node.Attributes["name"].Value);
                    CreateXML(node, newElement, ref xml);
                    if (element == null)
                        xml.AppendChild(newElement);
                    else
                        element.AppendChild(newElement);
                }
                if (node.Name == "xs:attribute")
                {
                    element.SetAttribute(node.Attributes["name"].Value, "");
                }
                if ((node.Name == "xs:complexType") || (node.Name == "xs:sequence") || (node.Name == "xs:schema"))
                    CreateXML(node, element, ref xml);
            }
        }
    }

How-to use

如何使用

XmlDocument xsd = new XmlDocument();
xsd.Load(xsdFileName);
XmlNode xsdNode = xsd.DocumentElement;
XmlElement element = null;
XmlDocument xml = new XmlDocument();
CreateXML(xsdNode, element, ref xml);

#1


12  

I think I googled it for you. Use XmlSampleGenerator from MSDN

我想我用Google搜索了它。使用MSDN中的XmlSampleGenerator

Sample use:

样品用途:

XmlTextWriter textWriter = new XmlTextWriter("po.xml", null);
textWriter.Formatting    = Formatting.Indented;
XmlQualifiedName qname   = new XmlQualifiedName("PurchaseOrder",       
                           "http://tempuri.org");
XmlSampleGenerator generator = new XmlSampleGenerator("po.xsd", qname);
genr.WriteXml(textWriter);

#2


3  

The problem has solved.

问题已经解决了。

private void CreateXML(XmlNode xsdNode, XmlElement element, ref XmlDocument xml)
    {
        if (xsdNode.HasChildNodes)
        {
            var childs = xsdNode.ChildNodes;
            foreach (XmlNode node in childs)
            {
                XmlElement newElement = null;
                if (node.Name == "xs:element")
                {
                    newElement = xml.CreateElement(node.Attributes["name"].Value);
                    CreateXML(node, newElement, ref xml);
                    if (element == null)
                        xml.AppendChild(newElement);
                    else
                        element.AppendChild(newElement);
                }
                if (node.Name == "xs:attribute")
                {
                    element.SetAttribute(node.Attributes["name"].Value, "");
                }
                if ((node.Name == "xs:complexType") || (node.Name == "xs:sequence") || (node.Name == "xs:schema"))
                    CreateXML(node, element, ref xml);
            }
        }
    }

How-to use

如何使用

XmlDocument xsd = new XmlDocument();
xsd.Load(xsdFileName);
XmlNode xsdNode = xsd.DocumentElement;
XmlElement element = null;
XmlDocument xml = new XmlDocument();
CreateXML(xsdNode, element, ref xml);