XML验证使用XSD在C#中不起作用

时间:2016-12-08 19:43:21

标签: c# xml xsd

我有一个XML,我需要验证它不是和序列,我写了一些代码,但它不起作用。

XML

c

XSD

word1 = 'ab'
word2 = 'ac'

m = [[0,1,2]
    ,[1,0,1]
    ,[2,1,_]]

 (i,j)
 1,1 => m[i][j] = m[i-1][j-1] = 0 // no change needed

 1,2 => min(m[0][1],m[1,1],m[0,2]) + 1
      = min(1      ,0     ,2     ) + 1 
      = 1
 choice represented: easiest to change 'a' to 'ac' by adding
                     1 ('c') to the solution for [i][j-1] = [1][1]

 2,1 => min(m[1][0],m[2][0],m[1][1]) + 1
      = min(1,     ,2      ,0      ) + 1
      = 1
 choice represented: easiest to change 'ab' to 'a' by adding
                     1 (deletion) to the solution for [i-1][j] = [1][1]

 2,2 => min(m[1][1],m[2][1],m[1][2]) + 1
      = min(0      ,1      ,1      ) + 1
      = _
 choice represented: you figure it out...

C#代码:

<?xml version="1.0" encoding="utf-8" ?>

<TXLife>
  <UserAuthRequest>
    <UserPswd>
      <CryptType>NONE</CryptType>
      <Pswd/>
    </UserPswd>
    <VendorApp>
      <VendorName>AAA</VendorName>
      <AppName>BBB</AppName>
    </VendorApp>
  </UserAuthRequest>
</TXLife>

正如您所看到的那样,我<?xml version="1.0" encoding="utf-8"?> <xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/NewApplicationSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="TXLife"> <xs:complexType> <xs:sequence> <xs:element name="UserAuthRequest" minOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="UserLoginName" type="xs:string" minOccurs="1"></xs:element> <xs:element name="UserPswd"> <xs:complexType> <xs:sequence> <xs:element name="CryptType" type="xs:string"></xs:element> <xs:element name="Pswd"></xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="VendorApp"> <xs:complexType> <xs:sequence> <xs:element name="VendorName"> <xs:complexType> <xs:attribute name="VendorCode" type="xs:int"></xs:attribute> </xs:complexType> </xs:element> <xs:element name="AppName" type="xs:string"></xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="VendorCode" type="xs:int"></xs:attribute> </xs:complexType> </xs:element> </xs:schema> 中没有提供public void ValidateXML1() { List<string> _errors = new List<string>(); try { XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("http://tempuri.org/NewApplicationSchema.xsd", _sourceXsd); XDocument doc = XDocument.Load(_sourceXml); string msg = ""; doc.Validate(schemas, (o, e) => { _errors.Add(e.Message); msg += e.Message + Environment.NewLine; }); } catch (Exception e) { } } ,而是<UserLoginName>我的XML,当我运行此代码时,它正在验证并显示XSD,但我预计错误不会提供inOccures=1值。

1 个答案:

答案 0 :(得分:1)

您的XML文件缺少架构命名空间。将其更改为

for (int i = 0; i < list.size() - 1; i++) {
            LatLng src = list.get(i);
            LatLng dest = list.get(i + 1);

            // mMap is the Map Object
            Polyline line = CalRout.mMap.addPolyline(
                    new PolylineOptions().add(
                            new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude,dest.longitude)
                    ).width(3).color(color)
            );
        }