使用MSDN Microsoft.Xml.XMLGen(XmlSampleGenerator API)将具有复杂类型的XSD转换为XML时出现的问题

问题描述 投票:-1回答:1

我正在尝试将具有复杂类型的XSD转换为XML我无法获得预期的结果。xsd如下所示。

但是只有一个节点显示正确的数据但是第二个没有显示正确的结果附上截图enter image description here任何想法吗?我可以通过在线进行转换,当时效果很好在samplegenerator中在多节点中使用相同的复杂类型是否有任何限制?

这里是XSD:

<xs:schema xmlns:xs="w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="XXX">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Applicant">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="PrimaryEmployment" type="EmploymentType"/>
                        <xs:element name="SecondEmployment" type="EmploymentType"/>
                    </xs:sequence>
                    <xs:attribute name="type" type="xs:string"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="EmploymentType">
    <xs:sequence>
        <xs:element name="TimeAtEmployer">
            <xs:complexType>
                <xs:sequence> 
                    <xs:element name="Years" type="xs:int"/>
                    <xs:element name="Months" type="xs:int" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

预期的xml文件

<Applicant>
<PrimaryEmployment>
  <TimeAtEmployer>
    <Years>123</Years>
    <Months>123</Months>
  </TimeAtEmployer>
</PrimaryEmployment>
<SecondEmployment>
  <TimeAtEmployer>
    <Years>123</Years>
    <Months>123</Months>
  </TimeAtEmployer>
</SecondEmployment>

当前XML

`<Applicant>
        <PrimaryEmployment>
          <TimeAtEmployer>
            <Years>123</Years>
            <Months>123</Months>
          </TimeAtEmployer>
        </PrimaryEmployment>
        <SecondEmployment>
          <TimeAtEmployer>      
          </TimeAtEmployer>
        </SecondEmployment>
      </Applicant>`

我尝试过的示例代码

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

Example2

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xml;
            var rootElement = new XmlQualifiedName("sampledata");
            var XSD = @"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" elementFormDefault=""qualified"" attributeFormDefault=""unqualified""> 
                <xs:element name = ""SampleData2"">

                <xs:complexType>

                <xs:sequence>

                <xs:element name = ""Applicant"">

                <xs:complexType>

                <xs:sequence>

                <xs:element name = ""PrimaryEmployment"" type = ""EmploymentType""/>

                <xs:element name = ""SecondEmployment"" type = ""EmploymentType""/>

                </xs:sequence>

                </xs:complexType>

                </xs:element>

                </xs:sequence>

                </xs:complexType>

                </xs:element>

                <xs:complexType name = ""EmploymentType"">

                <xs:sequence>

                <xs:element name = ""TimeAtEmployer"">

                <xs:complexType>

                <xs:sequence>

                <xs:element name = ""Years"" type = ""xs:int""/>

                <xs:element name = ""Months"" type = ""xs:int""/>

                </xs:sequence>

                </xs:complexType>

                </xs:element>

                </xs:sequence>
                </xs:complexType>
                </xs:schema>
                ";
            XmlElement serializedElement = Serialize(XSD);
        }
        public static XmlDocument GetXml(string xsd, XmlQualifiedName rootElem)
        {
            var reader = XmlReader.Create(new StringReader(xsd), new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Prohibit
            });

            XmlSchema schema = XmlSchema.Read(reader, null);
            var generator = new XmlSampleGenerator(schema, rootElem);
            generator.MaxThreshold = 2;

            var xmlStringBuilder = new StringBuilder();
            var writer = XmlWriter.Create(xmlStringBuilder);
            generator.WriteXml(writer);

            var resultXmlDoc = new XmlDocument();
            resultXmlDoc.LoadXml(xmlStringBuilder.ToString());
            //BlankOutValues(resultXmlDoc.DocumentElement);
            return resultXmlDoc;
        }
        public static XmlElement Serialize(object transformObject)
        {
            XmlElement serializedElement = null;

            MemoryStream memStream = new MemoryStream();
            XmlSerializer serializer = new XmlSerializer(transformObject.GetType());
            serializer.Serialize(memStream, transformObject);
            memStream.Position = 0;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(memStream);
            var reader = XmlReader.Create(new StringReader(new StreamReader(memStream).ReadToEnd().Replace("utf-8", "utf-16")), new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Prohibit
            });
            XmlDocument xsd = new XmlDocument();
            var rootElement = new XmlQualifiedName("LoanContract");
            xsd = GetXml(transformObject.ToString(), rootElement);
            return serializedElement;

        }
    }
}
c# xml xsd xml-parsing xsd-validation
1个回答
0
投票

我认为您需要更改XmlSampleGenerator.dll

private bool GenerateElement(XmlSchemaElement e, bool root, InstanceGroup parentElem, XmlSchemaAny any) {
         XmlSchemaElement eGlobalDecl = e;
         bool flagCached = false;
         if (!e.RefName.IsEmpty) {
               eGlobalDecl = (XmlSchemaElement)schemaSet.GlobalElements[e.QualifiedName];
         }
         if (!eGlobalDecl.IsAbstract) {
               InstanceElement elem = (InstanceElement)elementTypesProcessed[eGlobalDecl];
               if (elem != null) {
                   Debug.Assert(!root);
                   if (any == null && e.MinOccurs > 0) { //If not generating for any or optional ref to cyclic global element
                       decimal occurs = e.MaxOccurs;
                       if (e.MaxOccurs >= maxThreshold) {
                           occurs = maxThreshold;
                       }
                       if (e.MinOccurs > occurs) {
                           occurs = e.MinOccurs;
                       }
                       elem = elem.Clone(occurs);
                       parentElem.AddChild(elem);
                       flagCached = true;
                   }
               }
               else
               {
                   elem = new InstanceElement(eGlobalDecl.QualifiedName);
               }
               if(root) {
                   instanceRoot = elem;
               }
               else {
                   if(!flagCached)
                   parentElem.AddChild(elem);
                   flagCached = false;
               }
               //Get minOccurs, maxOccurs alone from the current particle, everything else pick up from globalDecl
               if (any != null) { //Element from any
                   elem.Occurs = any.MaxOccurs >= maxThreshold ? maxThreshold : any.MaxOccurs;
                   elem.Occurs = any.MinOccurs > elem.Occurs ? any.MinOccurs : elem.Occurs;
               }
               else {
                   elem.Occurs = e.MaxOccurs >= maxThreshold ? maxThreshold : e.MaxOccurs;
                   elem.Occurs = e.MinOccurs > elem.Occurs ? e.MinOccurs : elem.Occurs;
               }
               elem.DefaultValue = eGlobalDecl.DefaultValue;
               elem.FixedValue = eGlobalDecl.FixedValue;
               elem.IsNillable = eGlobalDecl.IsNillable;
               if (eGlobalDecl.ElementSchemaType == AnyType) {
                   elem.ValueGenerator = XmlValueGenerator.AnyGenerator;
               }
               else {
                   XmlSchemaComplexType ct = eGlobalDecl.ElementSchemaType as XmlSchemaComplexType;
                   if (ct != null) {
                       if(!elementTypesProcessed.ContainsKey(eGlobalDecl)) elementTypesProcessed.Add(eGlobalDecl, elem);
                       if (!ct.IsAbstract) {
                           elem.IsMixed = ct.IsMixed;
                           ProcessComplexType(ct, elem);
                       }
                       else { // Ct is abstract, need to generate instance elements with xsi:type
                           XmlSchemaComplexType dt = GetDerivedType(ct);
                           if (dt != null) {
                               elem.XsiType = dt.QualifiedName;
                               ProcessComplexType(dt, elem);
                           }
                       }
                   }
                   else { //elementType is XmlSchemaSimpleType
                       elem.ValueGenerator = XmlValueGenerator.CreateGenerator(eGlobalDecl.ElementSchemaType.Datatype, listLength);
                   }
               }
               if (elem.ValueGenerator != null && elem.ValueGenerator.Prefix == null) {
                   elem.ValueGenerator.Prefix = elem.QualifiedName.Name;
               }
               return true;
         } // End of e.IsAbstract
         return false;
       }

它正在为我工​​作,请检查

© www.soinside.com 2019 - 2024. All rights reserved.