如何使用XsdDataContractExporter架构验证DataContract?

时间:2012-08-14 13:25:16

标签: c# xml wcf xml-serialization xsd

我有一个使用DataContractSerializer生成的配置文件,然后使用XsdDataContractExporter生成模式,但架构不会验证我的xml。如何生成可用于外部验证XML的有效XSD?

我的Xml:

    <?xml version="1.0" encoding="utf-8"?>
<ConfigProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions">
  <ConfigSections xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:anyType i:type="IISSection">
      <AnonymousUser xmlns:d4p1="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions.Section.User">
        <d4p1:UserDescription i:nil="true" />
        <d4p1:UserGroup i:nil="true" />
        <d4p1:UserName>Anon</d4p1:UserName>
        <d4p1:UserPassword>testpass</d4p1:UserPassword>
        <d4p1:UserType>Anonymous</d4p1:UserType>
      </AnonymousUser>
      <ApplicationPoolName>AppPool</ApplicationPoolName>
      <Port>8080</Port>
      <VirtualDirectoryAlias>/virtdir</VirtualDirectoryAlias>
      <WebsiteName>Default</WebsiteName>
      <WebsiteRoot>C:\SomePath</WebsiteRoot>
    </d2p1:anyType>
    <d2p1:anyType xmlns:d3p1="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions.Section.WebConfig" i:type="d3p1:WebConfigSection">
      <d3p1:CommonItems>
        <d3p1:WebConfigItem>
          <d3p1:Data>
            <add key="serverurl" value="http://myserverurl" xmlns="" />
          </d3p1:Data>
          <d3p1:Name>serverurl</d3p1:Name>
          <d3p1:XPath>//add[@key='serverurl']</d3p1:XPath>
        </d3p1:WebConfigItem>
        <d3p1:WebConfigItem>
          <d3p1:Data>
            <authentication mode="Forms" xmlns="">
              <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" protection="All" timeout="4800" path="/" />
            </authentication>
          </d3p1:Data>
          <d3p1:Name>Authentication</d3p1:Name>
          <d3p1:XPath>//system.web/authentication[@mode]</d3p1:XPath>
        </d3p1:WebConfigItem>
        <d3p1:WebConfigItem>
          <d3p1:Data>
            <pages theme="AvantGarde" clientIDMode="AutoID" xmlns="" />
          </d3p1:Data>
          <d3p1:Name>Pages</d3p1:Name>
          <d3p1:XPath>//system.web/pages</d3p1:XPath>
        </d3p1:WebConfigItem>
      </d3p1:CommonItems>
      <d3p1:FileLocation>C:\my\web.config</d3p1:FileLocation>
      <d3p1:OverRideItems i:nil="true" />
      <d3p1:UserDifference>
        <d3p1:WebConfigItem>
          <d3p1:Data>
            <add key="serverurl" value="http://myserverurl" xmlns="" />
          </d3p1:Data>
          <d3p1:Name>add</d3p1:Name>
          <d3p1:XPath i:nil="true" />
        </d3p1:WebConfigItem>
        <d3p1:WebConfigItem>
          <d3p1:Data>
            <identity impersonate="true" userName="Worker" password="password" xmlns="" />
          </d3p1:Data>
          <d3p1:Name>identity</d3p1:Name>
          <d3p1:XPath i:nil="true" />
        </d3p1:WebConfigItem>
      </d3p1:UserDifference>
      <d3p1:WebConfig>
        <configuration xmlns="">
          <appSettings>
            <!-- EXAMPLE Server URL to post to -->
            <add key="serverurl" value="http://myserverurl" />
            <!-- USDemoUserDetailManuScript The ManuScript ID to edit advanced user details -->
            <add key="UserDetailsManuScriptID" value="" />
            <!-- The ManuScript ID to edit advanced agency details -->
            <add key="AgencyDetailsManuScriptID" value="" />
            <!-- The return address for emails sent -->
            <add key="MailTo" value="support@mycompany.com" />
            <!-- SMTPServer name to use while sending emails -->
            <add key="SMTPServer" value="" />
            <add key="TimeOut" value="300000" />
            <add key="OnAddDuplicateEntity" value="Error" />
            <!-- The expected behavior when entered entity data is identical to an existing entity - options: Error, Continue -->
            <add key="StatesXML" value="States" />
          </appSettings>
          <connectionStrings />
          <system.web>
            <authentication mode="Forms">
              <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" protection="All" timeout="4800" path="/" />
            </authentication>
            <authorization>
              <deny users="?" />
            </authorization>
            <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
            <pages theme="AvantGarde" clientIDMode="AutoID" />
            <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="200" />
            <xhtmlConformance mode="Legacy" />
            <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
            <identity impersonate="true" userName="Worker" password="password" />
          </system.web>
          <system.webServer>
            <security>
              <requestFiltering>
                <hiddenSegments>
                  <add segment="App_XSLT" />
                </hiddenSegments>
              </requestFiltering>
            </security>
          </system.webServer>
          <location path="scripts">
            <system.web>
              <authorization>
                <allow users="*,?" />
              </authorization>
            </system.web>
          </location>
          <location path="App_Themes">
            <system.web>
              <authorization>
                <allow users="*,?" />
              </authorization>
            </system.web>
          </location>
        </configuration>
      </d3p1:WebConfig>
      <d3p1:WorkerProcessUser xmlns:d4p1="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions.Section.User">
        <d4p1:UserDescription i:nil="true" />
        <d4p1:UserGroup i:nil="true" />
        <d4p1:UserName>Worker</d4p1:UserName>
        <d4p1:UserPassword>password</d4p1:UserPassword>
        <d4p1:UserType>WorkerProcess</d4p1:UserType>
      </d3p1:WorkerProcessUser>
    </d2p1:anyType>
  </ConfigSections>
  <InstanceName i:nil="true" />
  <ProfileDesc>Current Settings</ProfileDesc>
  <ProfileId i:nil="true" />
  <ProfileName>validate</ProfileName>
  <ProfileType>CustomSettings</ProfileType>
</ConfigProfile>

生成我的架构:

    <?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  <xs:import namespace="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions.Section.User" />
  <xs:complexType name="ConfigProfile">
    <xs:sequence>
      <xs:element minOccurs="0" name="ConfigSections" nillable="true" xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" type="q1:ArrayOfanyType" />
      <xs:element minOccurs="0" name="InstanceName" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="ProfileDesc" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="ProfileId" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="ProfileName" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="ProfileType" type="tns:ConfigProfile.ProfileTypeEnum" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ConfigProfile" nillable="true" type="tns:ConfigProfile" />
  <xs:simpleType name="ConfigProfile.ProfileTypeEnum">
    <xs:restriction base="xs:string">
      <xs:enumeration value="CurrentSettings" />
      <xs:enumeration value="CustomSettings" />
      <xs:enumeration value="DefaultSettings" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="ConfigProfile.ProfileTypeEnum" nillable="true" type="tns:ConfigProfile.ProfileTypeEnum" />
  <xs:complexType name="IISSection">
    <xs:sequence>
      <xs:element minOccurs="0" name="AnonymousUser" nillable="true" xmlns:q2="http://schemas.datacontract.org/2004/07/ConfigurationUtilityFunctions.Section.User" type="q2:User" />
      <xs:element minOccurs="0" name="ApplicationPoolName" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="Port" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="VirtualDirectoryAlias" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="WebsiteName" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="WebsiteRoot" nillable="true" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="IISSection" nillable="true" type="tns:IISSection" />
</xs:schema>

0 个答案:

没有答案