실제로 xml에 추가 네임 스페이스 설정이 없지만 네임 스페이스 'http : // yyy'의 하위 요소 'xxx'가 잘못되었습니다.

서클 샤오

그래서 비슷한 토론이 많았지 만 약 5-6 개의 게시물을 살펴본 후. 그들 중 어느 것도 내 사건과 일치하지 않는 것 같습니다. 내 관찰에 따르면 대부분의 이전 게시물에는 문제를 일으키는 추가 네임 스페이스 설정이있는 것으로 보이지만 내 사례가 동일하다고 생각하지 않습니다.

그래서 다음 xml을 수신하는 웹 서비스가 있습니다.

<?xml version="1.0" encoding="UTF-8"?>
<ProductBindingReportRequest xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <CustomerBindingUniqueID>FA8E995F-50D2E911-9475-0050560063F7</CustomerBindingUniqueID>
   <SoldToCustomerID>0000064397</SoldToCustomerID>
   <ReceivedFromCustomerID>QACN-CQ</ReceivedFromCustomerID>
   <TotalLineItems>2</TotalLineItems>
   <ProductBindings>
      <ProductBinding>
         <YZProductKeyID>3423553504516</YZProductKeyID>
         <XYSpecifics>
            <XYSerialNumber>5CD936C2FW</XYSerialNumber>
            <ConsumedDate>9/8/2019 3:47:37 PM</ConsumedDate>
         </XYSpecifics>
         <ServiceProductKeyID>4020124189204</ServiceProductKeyID>
      </ProductBinding>
      <ProductBinding>
         <YZProductKeyID>3423553504661</YZProductKeyID>
         <XYSpecifics>
            <XYSerialNumber>5CD936C1KK</XYSerialNumber>
            <ConsumedDate>9/8/2019 3:47:55 PM</ConsumedDate>
         </XYSpecifics>
         <ServiceProductKeyID>4020124189205</ServiceProductKeyID>
      </ProductBinding>
   </ProductBindings>
   <XYSpecifics>
      <SiteIdentifier>QACN-CQ</SiteIdentifier>
   </XYSpecifics>
</ProductBindingReportRequest>

xml 입력의 유효성을 검사해야하므로 위의 xml을 기반으로 다음 xsd를 만듭니다.

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="ProductBindingReportRequest">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
        <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
        <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
        <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
        <xsd:element name="ProductBindings">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="ProductBinding">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                    <xsd:element name="XYSpecifics">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="XYSerialNumber" type="xsd:string" />
                          <xsd:element name="ConsumedDate" type="xsd:string" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="XYSpecifics">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="SiteIdentifier" type="xsd:string" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

그리고 아래 방법으로 xsd로 xml의 유효성을 검사합니다.

public static bool ValidateXML(string strXML, string strSchemePath, string strXMLSchemeName, ref string strException)
{
    if (strSchemePath.Substring(strSchemePath.Length - 1) != "\\")
        strSchemePath = strSchemePath + "\\";

    try
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);
        settings.ValidationType = ValidationType.Schema;
        XmlReader readerCheck = XmlReader.Create(new StringReader(strXML), settings);
        XmlDocument checkXmlDocument = new XmlDocument();
        checkXmlDocument.Load(readerCheck);
        checkXmlDocument.RemoveAll();
        return true;
    }
    catch (Exception ex)
    {
        strException = ex.ToString();
        return false;
    }
}

수신 된 대부분의 xml은 동일한 구현으로 잘 작동하지만 위의 조합은 나에게

네임 스페이스 'http : //schemas.ms.it.oem/digitaldistribution/2010/10'의 'XYSpecifics'요소에 'http : //schemas.ms.it.oem/digitaldistribution/ 네임 스페이스에 잘못된 자식 요소'SiteIdentifier '가 있습니다. 2010/10 '.

내 질문은

  1. XYSpecifics내부와 외부 모두 존재 하기 때문에 ProductBindings?
  2. 유효성 검사가 작동하도록 xsd를 조정할 수있는 방법이 있습니까?
마티아스 뮬러

나는 완전히 확실하지 않다

settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);

XML 판독기 인스턴스에 영향을 미치지 만 XML 스키마 문서 자체에이 네임 스페이스를 포함 할 수도 있습니다.

XML 스키마

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10">
    <xsd:element name="ProductBindingReportRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
                <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
                <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
                <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
                <xsd:element name="ProductBindings">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="ProductBinding">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                                        <xsd:element name="XYSpecifics">
                                            <xsd:complexType>
                                                <xsd:sequence>
                                                    <xsd:element name="XYSerialNumber" type="xsd:string" />
                                                    <xsd:element name="ConsumedDate" type="xsd:string" />
                                                </xsd:sequence>
                                            </xsd:complexType>
                                        </xsd:element>
                                        <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="XYSpecifics">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="SiteIdentifier" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

XYSpecifics스키마에서 요소 가 다른 자식 요소와 함께 두 번 정의 되는 것은 조금 안타깝지만 작동 합니다. 스키마를 작성하는 더 좋은 방법은이 유형을 한 번 정의하는 것입니다. 예를 들면 :

XML 스키마 (개선됨)

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10"
    xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10">
    <xsd:element name="ProductBindingReportRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
                <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
                <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
                <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
                <xsd:element name="ProductBindings">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="ProductBinding">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                                        <xsd:element name="XYSpecifics" type="XYSpecificsType"/>
                                        <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="XYSpecifics" type="XYSpecificsType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    
    <xsd:complexType name="XYSpecificsType">
        <xsd:choice maxOccurs="3">
            <xsd:element name="XYSerialNumber" type="xsd:string" />
            <xsd:element name="ConsumedDate" type="xsd:string" />
            <xsd:element name="SiteIdentifier" type="xsd:string" />
        </xsd:choice>
    </xsd:complexType>
    
</xsd:schema>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관