用XmlTextReader读取包含正则表达示的XML文件时出错,错误内容:未处理的“System.Xml.XmlException”类型的异常出现在 system.xml.dll 中。其他信息: 系统错误。
同时用VS自动生成Schema时出错"分析 EntityName 时出错,出错处请看XML文件。
我的代码如下:if (trv.MoveToContent() == XmlNodeType.Element) // make sure the node is element.
{ if (trv.Name == "RegulationType") // make sure it's RegulationType node.
{
type = new RegulationType();
regulationTypes.Add(type);
}
else if (type != null) // make sure RegulationType object already exsited.
{
if (trv.Name == "Name") // it's Name node.
type.Name = trv.ReadString();
else if (trv.Name == "Regex") // it's Regex node.
type.RegularExpression = trv.ReadString(); //这行出错!!!
} trv.Read(); // read on.
}
else
trv.Read(); // read on.XML文件如下:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://tempuri.org/RegulationTypes.xsd">
  <RegulationType>
    <Name>E-mail类型</Name>
    <Regex>^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$</Regex>
  </RegulationType>
  <RegulationType>
    <Name>URL类型</Name>
    <Regex>http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?</Regex>     用VS自动生成Schema时出错"分析 EntityName 时出错. 行 9, 位置 48 (就是这行里的&)
  </RegulationType>
</root>XSD的Schema如下:
<?xml version="1.0" ?>
<xs:schema id="root" targetNamespace="http://tempuri.org/RegulationTypes.xsd" xmlns:mstns="http://tempuri.org/RegulationTypes.xsd"
xmlns="http://tempuri.org/RegulationTypes.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="root" msdata:IsDataSet="true" msdata:Locale="zh-CN" msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="RegulationType">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Regex" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

解决方案 »

  1.   

    这是我在XML版开的另一个贴子(50分), 如果在此版答复成功者可到下面的贴子UP一个领另外50分, 另外, UP者有分 :)
    http://community.csdn.net/Expert/topic/4303/4303840.xml?temp=.2774164
      

  2.   

    正则表达式部份可以考虑用Base64编码
    先把正则表达式内容用UTF8编码格式转为byte[]
    在写入XML时就可以用Base64来编码出来了,
    我想因为正则表达式中很可能会含有XML中敏感的字符,还是编码后好点