我在这里感到困惑...尝试使用Schema解析一个简单的XML文件,我收到错误。
这是一个简单的代码:
xmlDoc.LoadXml(retXML);
txtRead = new StringReader(xmlDoc.InnerXml);
tmpDS = new DataSet();
tmpDS.ReadXmlSchema(HttpContext.Current.Server.MapPath("~/schemas/pnrView.xml"));
tmpDS.ReadXml(txtRead);
然后我收到了这个错误:
输入字符串的格式不正确。在System.Data.Scom.DevimalStorage.ConvertXmlToObject处的System.Number.ParseDecimal(String value,NumberStyles options,NumberFormatInfo numfmt)处的System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal)处(字符串s)System.Data.XmlDataLoader.LoadColumn(DataColumn列,Object [] foundColumns),位于System.Data.XmlDataLoader.LoadTable(DataTable表,布尔值为isBested)的System.Data.XmlDataLoader.LoadTable(DataTable表,Boolean isNested)处。在DotNetNuke.StSystem.Bookings.GetPnrView(NameValueCollection formData)的System.Data.DataSet.ReadXml(XmlReader reader,Boolean denyResolving)的System.Data.XmlDataLoader.LoadData(XmlReader reader)中
这是xml:
<PNRView>
<PNR>
<PricePPPN_UserCurrency/>
<PricePP_UserCurrency/>
<PriceTotal_UserCurrency/>
<PricePPPN_LocalCurrency/>
<PricePP_LocalCurrency/>
<LocalCurrency>EUR</LocalCurrency>
<PrCur>EUR</PrCur>
<PrTotal>0.0000</PrTotal>
<PrTotalEUR>0.0000</PrTotalEUR>
<ServiceNotes/>
</PNR>
</PNRView>
这是架构:
<?xml version="1.0" standalone="yes"?>
<xs:schema id="PNRView" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="PNRView" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="PNR">
<xs:complexType>
<xs:sequence>
<xs:element name="PricePPPN_UserCurrency" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="PricePP_UserCurrency" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="PriceTotal_UserCurrency" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="PricePPPN_LocalCurrency" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="PricePP_LocalCurrency" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="LocalCurrency" type="xs:string" minOccurs="0" />
<xs:element name="PrCur" type="xs:string" minOccurs="0" />
<xs:element name="PrTotal" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="PrTotalEUR" type="xs:decimal" minOccurs="0" default="0" nillable="true" />
<xs:element name="ServiceNotes" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我尝试了minoccurs / nillable / default的每个组合,xml在FreeFormater.com中通过了验证,但C#有不同的意见。
答案 0 :(得分:0)
属性nillable="true"
和minOccurs="0"
仅在没有元素发送时有效。要允许将空值作为值,必须使用union来允许整数值,并且如果element为空,则必须使用case。
<xs:element name="PrTotal">
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base='xs:integer'>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>