微软Url重写源码         
        public object Create(object parent, object configContext, System.Xml.XmlNode section) 
{
// Create an instance of XmlSerializer based on the RewriterConfiguration type...
XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration)); // Return the Deserialized object from the Web.config XML
return ser.Deserialize(new XmlNodeReader(section.SelectSingleNode("test")));
}          webconfig
             <RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>/Company/([\w]*)[/]{1,}([\w]*\.html)</LookFor>
        <SendTo>/Company/index.aspx?Id={1}</SendTo>
      </RewriterRule>
     <RewriterRule2>
        <LookFor>/Company/([\w]*)[/]{1,}([\w]*\.html)</LookFor>
        <SendTo>/Company/index.aspx?Id={1}</SendTo>
      </RewriterRule2>
     </Rules>
  </RewriterConfig>我只需要RewriterRule2的内容,用section.SelectSingleNode("RewriterRule2"))反序列时就出错.{"不应有 <RewriterRule2 xmlns=''>。"}这样的提示请问如何解决

解决方案 »

  1.   

    第三个参数System.Xml.XmlNode section 你传的是什么?
      

  2.   

    继承IConfigurationSectionHandler 
    public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler 
    {
    /// <summary>
    /// Creates an instance of the <see cref="RewriterConfiguration"/> class.
    /// </summary>
    /// <res>Uses XML Serialization to deserialize the XML in the Web.config file into an
    /// <see cref="RewriterConfiguration"/> instance.</res>
    /// <returns>An instance of the <see cref="RewriterConfiguration"/> class.</returns>
    public object Create(object parent, object configContext, System.Xml.XmlNode section) 
    {
    // Create an instance of XmlSerializer based on the RewriterConfiguration type...
    XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration)); // Return the Deserialized object from the Web.config XML
                return ser.Deserialize(new XmlNodeReader(section));
    } }
      

  3.   


    使用XML序列化
        在.NET框架里利用内置的XML序列化方法相对较为容易。你只需要熟悉一些类和属性就可以开始使用简单的XML序列化了:System.Xml.Serialization命名空间:含有使用XML序列化所需要的类和功能。这个命名空间应该被放在使用XML序列化的类的顶部的一个“using”命令里。
    XmlSerializer类:提供将对象序列化和反序列化的功能。
    XmlIgnore属性:告诉XmlSerializer类跳过你不希望序列化的成员。//这个属性只有通过 构造函数传参数才会有作用Class3
    [Serializable()]
        public class Class2
        {
            private string _userName = string.Empty;
            public string UserName
            {
                get
                {
                    return _userName;
                }
                set
                {
                    _userName = value;
                }
            }
            public Class3 vv;    }[Serializable]
        public class Class3
        {
            private string _kk=String.Empty;        [XmlIgnore]private string _Description=String.Empty;
            public string kk
            {
                set
                {
                    _kk = value;
                }
                get
                {
                    return _kk;
                }
            }
            public string Description
            {
                set
                {
                    _Description = value;
                }
                get
                {
                    return _Description;
                }
            }        //public string Description
            //{
            //    set
            //    {
            //        _Description = value;
            //    }
            //    get
            //    {
            //        return _Description;
            //    }
            //}
            public Class3() { }
            public Class3(string d)
            {
                _Description = d;
            }
    这样跳过你不希望序列化的成员
       
        }Class2 cls = new Class2();
                cls.UserName = "xingming!";
                cls.vv = new Class3();
                cls.vv.kk = "lovelove";
                cls.vv.Description = "miaoshu";
         
                XmlSerializer xs = new XmlSerializer(typeof(Class2));
                Stream stream = new FileStream("f:\\1.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                xs.Serialize(stream, cls);
                stream.Close();XmlSerializer xs = new XmlSerializer(typeof(Class2));
                Stream stream = new FileStream("f:\\1.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                Class2 p = (Class2)xs.Deserialize(stream);
                MessageBox.Show(p.UserName.ToString());