gpt4 book ai didi

c# - 命名空间 'xxx' 中的子元素 'http://yyy' 无效,而实际上 xml 中没有额外的命名空间设置

转载 作者:行者123 更新时间:2023-12-04 15:17:13 27 4
gpt4 key购买 nike

SO 有很多类似的讨论,但在查看了大约 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 都可以在相同的实现下正常工作,但上面的组合给了我

The element 'XYSpecifics' in namespace'http://schemas.ms.it.oem/digitaldistribution/2010/10' has invalidchild element 'SiteIdentifier' in namespace'http://schemas.ms.it.oem/digitaldistribution/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>

关于c# - 命名空间 'xxx' 中的子元素 'http://yyy' 无效,而实际上 xml 中没有额外的命名空间设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64114753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com