1

コンマ区切りの値 (およびそのラベル) の文字列をメモリから読み取り、それらを XML ファイルにパッケージ化して送信するサービスを作成しています。関連する XSD ファイルは、各 XML 構造を持つデータ所有者によって生成されています。.txtWeb サービス経由で XML を送信する前に、XSD ファイルを読み込んで、ファイル内のデータ ラベルのリストを検証したいと考えています。XSD と txt ファイルの間で不一致が検出された場合、コードで例外をスローするようにしたいと考えています。一致が成功すると、.txtファイル、パッケージから値を取得し、XML 要求を送信します。これを行う方法のプロセス全体 (入力から出力まで) の例を探しています。

入力ファイルを読み取り、属性と値を収集します (TestValuesIN.txt)

 "test.Foo", 122;

XSD ファイルを開き、入力ファイルの要素のリストに対して要素を 1 対 1 で照合します。今のところ、XSD に存在する "Test.Foo" (要素: Test.Foo val:) だけを気にします。(TextXSD.xsd)

<?xml version="1.0"?>
<!-- Generated using Flame-Ware Solutions XML-2-XSD v2.0 at http://www.flame-ware.com/Products/XML-2-XSD/ -->
<xs:schema id="batch-execution" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="batch-execution" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="insert">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="test.Foo" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="val" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="fire-all-rules">
                    <xs:complexType>
                        <xs:attribute name="max" type="xs:string" />
                    </xs:complexType>
                </xs:element>
                <xs:element name="get-objects">
                    <xs:complexType>
                        <xs:attribute name="out-identifier" type="xs:string" />
                    </xs:complexType>
                </xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

次に、XML をポストします。

http://mycamelhump.<myurl>.com:8080/drools-camel-service/kservice/CommTestXML/execute 

URI

<batch-execution>
 <insert>
  <test.Foo>
    <val>122</val>
  </test.Foo>
</insert>
<fire-all-rules max="-1"/>
<get-objects out-identifier="also-foo"/>
</batch-execution>

コードは結果を取得し、出力を (TestValuesOUT.txt) に出力します。

"Test.Foo", 100;
4

1 に答える 1

0

次のように、xml ドキュメントを作成し、.NET XML スキーマセットを使用して xml を検証してから、Web サービスに投稿するとどうなるでしょうか。

public List<string> ValidateXml(string xml, string rootXsdName)
    {
        List<string> xsdValidationErrors = new List<string>();

        try
        {
            // Note: Don't use XDocument schema validation as this will give a false positive of a string without XSD specified       
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
            settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += ((o, e) => xsdValidationErrors.Add(e.Message));
            settings.Schemas = GetXmlSchemas(rootXsdName);

            TextReader textReader = new StringReader(xml);
            XmlReader reader = XmlReader.Create(textReader, settings);

            // Parse the file.          
            while (reader.Read())
            {
                //Empty control loop to read through entire document getting errors uaing ValidationEventHandler
            }
        }
        catch (Exception ex)
        {
            xsdValidationErrors.Add("Unable to parse XML");
        }


        return xsdValidationErrors;
    }

GetXmlSchemas の実装は次のようになります...

  /// <summary>
    /// Generates an XmlSchema set to use to validate the xDocument
    /// </summary>
    private XmlSchemaSet GetXmlSchemas(string xsdFileName)
    {
        XmlSchemaSet schemas = new XmlSchemaSet();

        // ... build the schema set here

        return schemas;
    }

そのため、GetXmlSchemaSet はすべての xsd ファイルにロードされます (複数のファイルがある場合)。

HTH

于 2013-02-19T19:19:09.643 に答える