xml schema递归应用

例如有如下xml文件

<?xml version="1.0" encoding="utf-8" ?>
<TreeView Version="1.0"  xmlns="http://example.books.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://example.books.com SchemaTest.xsd">
  <Position Detail="1">
    <PositionName>1</PositionName>
    <SubPosition Detail="2">
      <PositionName>2</PositionName>
      <SubPosition Detail="3">
        <PositionName>3</PositionName>
        <SubPosition Detail="4">
          <PositionName>4</PositionName>
        </SubPosition>
      </SubPosition>
    </SubPosition>
  </Position>
</TreeView>
每个SubPosition 都可以包含自己

那么schema应该怎么写了

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.com" xmlns="http://example.books.com" targetNamespace="http://example.books.com" elementFormDefault="qualified">
<xs:element name="TreeView">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="Position" type="PositionType"/>
      </xs:sequence>
      <xs:attribute name="Version" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="PositionType">
    <xs:sequence>
      <xs:element name="PositionName" type="xs:string"/>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="SubPosition" type="PositionType"/>
      </xs:sequence>
    </xs:sequence>
    <xs:attribute name="Detail" type="xs:string"/>
  </xs:complexType>
</xs:schema>

 事例二

<?xml version="1.0" encoding="utf-8" ?>
  <TreeNode text="系统导航" value="0A" description="系统导航" url="Default.aspx"  xmlns="http://example.books.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://example.books.com SchemaTest.xsd">
    <TreeNode text="系统管理" value="0A" description="系统管理" url="">
      <TreeNode text="角色权限管理" value="0A0A" description="角色权限管理" url="./Admin/ManagerRole.aspx" />
      <TreeNode text="常量表管理" value="0A0B" description="常量表管理" url="./Admin/ManageCodeTables.aspx"/>
      <TreeNode text="日志管理" value="0A" description="日志管理" url="./Admin/ExceptionLogViewer.aspx"/>
    </TreeNode>
    <TreeNode text="用户管理" value="0B" description="用户管理" url="./User/ManageUserInfo.aspx"/>
  </TreeNode>

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.com" xmlns="http://example.books.com" targetNamespace="http://example.books.com" elementFormDefault="qualified">
  <xs:element name="TreeNode">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded" minOccurs="1">
        <xs:element name="TreeNode" type="TreeNodeType"/>
      </xs:sequence>
      <xs:attribute name="text" type="xs:string" use="required" />
      <xs:attribute name="value" type="xs:string" use="required" />
      <xs:attribute name="description" type="xs:string" use="required" />
      <xs:attribute name="url" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
  <xs:complexType name="TreeNodeType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="TreeNode" type="TreeNodeType"/>
    </xs:sequence>
    <xs:attribute name="text" type="xs:string" use="required" />
    <xs:attribute name="value" type="xs:string" use="required" />
    <xs:attribute name="description" type="xs:string" use="required" />
    <xs:attribute name="url" type="xs:string" use="required" />
  </xs:complexType>
</xs:schema>

 

C#的验证代码:

   string schemafile = this.textBoxschema.Text.Trim();
            string xmlfile = this.textBoxxml.Text.Trim();
            XmlSchemaSet schema = new XmlSchemaSet();
            schema.Add(null, XmlReader.Create(schemafile));
            XDocument doc = XDocument.Load(xmlfile);
            doc.Validate(schema, (senderParam, eParam) => {
                richTextBoxResult.Text = eParam.Message;
                isValid = false;
            }, true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值