XML篇---可配置化的取值方式[便于维护]

1.根据XML的架构图,用xsd生成相应于的cs文件

None.gif 命令为:xsd /c CooperationChannels.xsd
None.gif以下是CMD的结果图
None.gif
None.gifC:\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd /c CooperationChannels.xsd
None.gifMicrosoft (R) Xml Schemas/DataTypes support utility
None.gif[Microsoft (R) .NET Framework, Version 2.0.50727.42]
None.gifCopyright (C) Microsoft Corporation. All rights reserved.
None.gifWriting file 'C:\Microsoft Visual Studio 8\SDK\v2.0\Bin\CooperationChannels.cs'.

XSD关系图:

None.gif 相对应的xml文件
None.gif
None.gif
<? xml version="1.0" encoding="utf-8"  ?>
None.gif
< CooperationChannel >
None.gif 
< Channel  DomainName ="crp"  Remark ="XXX" >
None.gif  
< Url  Name ="www.baidu.com"  BaseTitle =""  UrlRemark ="" />
None.gif  
< Url  Name ="mail.163.com"  BaseTitle =""  UrlRemark ="" />
None.gif  
< Url  Name ="XXXXX"  BaseTitle =""  UrlRemark ="" />
None.gif  
< Url  Name ="XXYYYY"  BaseTitle =""   UrlRemark ="XXXX" />
None.gif 
</ Channel >
None.gif
</ CooperationChannel >

2.由于生成的文件,不能直接使用,所以需要对xsd生成的文件进行相关的修改

以下是修改的代码,注意注释

ContractedBlock.gif ExpandedBlockStart.gif
None.gif//------------------------------------------------------------------------------
None.gif
// <auto-generated>
None.gif
//     This code was generated by a tool.
None.gif
//     Runtime Version:2.0.50727.42
None.gif
//
None.gif
//     Changes to this file may cause incorrect behavior and will be lost if
None.gif
//     the code is regenerated.
None.gif
// </auto-generated>
None.gif
//------------------------------------------------------------------------------
None.gif

None.gif
using System.Xml.Serialization;
None.gif
None.gif
// 
None.gif
// This source code was auto-generated by xsd, Version=2.0.50727.42.
None.gif
// 
None.gif

None.gif[System.CodeDom.Compiler.GeneratedCodeAttribute(
"xsd""2.0.50727.42")]//可删除
None.gif
[System.SerializableAttribute()]
None.gif[System.Diagnostics.DebuggerStepThroughAttribute()]
//可删除
None.gif
[System.ComponentModel.DesignerCategoryAttribute("code")]//可删除
None.gif
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
None.gif
//修改前的生成代码
None.gif
//[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/CooperationChannels.xsd", IsNullable=false)]
None.gif
//修改后的代码
None.gif
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
None.gif
public partial class CooperationChannel
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
//修改前的生成代码
InBlock.gif    
//private Channel channelField;
InBlock.gif    
//修改后的代码
InBlock.gif
    private Channel[] channelField;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 关系说明
InBlock.gif    
/// XmlRootAttribute//根
InBlock.gif    
/// XmlElementAttribute//节点
InBlock.gif    
/// XmlAttributeAttribute//节点上的属性
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
///需要添加属性,[必须要添加的,不然无法确定xml中的关系]

InBlock.gif    [System.Xml.Serialization.XmlElementAttribute("Channel")]
InBlock.gif    
//public Channel Channel //修改前的生成代码
InBlock.gif
    public Channel[] Channel //修改后的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.channelField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.channelField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Channel#region Channel
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 便于取值 Channel索引器
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="_url"></param>
ExpandedSubBlockEnd.gif    
/// <returns>返回相关的Url</returns>

InBlock.gif    public Channel this[string _url]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (Channel channel in channelField)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (Url url in channel.Url)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (url.Name.Equals(_url))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return channel;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <remarks/>
None.gif[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd""2.0.50727.42")]//可删除
None.gif
[System.SerializableAttribute()]
None.gif[System.Diagnostics.DebuggerStepThroughAttribute()]
//可删除
None.gif
[System.ComponentModel.DesignerCategoryAttribute("code")]//可删除
None.gif
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
None.gif
//修改前的生成代码
None.gif
//[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/CooperationChannels.xsd", IsNullable=false)]
None.gif
//修改后的代码
None.gif
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
None.gif
public partial class Channel
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// private Url urlField; //修改前的生成代码
InBlock.gif
    private Url[] urlField; //修改后的代码
InBlock.gif

InBlock.gif    
private string domainNameField;
InBlock.gif
InBlock.gif    
private string remarkField;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlElementAttribute("Url")]
InBlock.gif    
//public Url Url  //修改前的生成代码
InBlock.gif
    public Url[] Url  //修改后的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.urlField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.urlField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlAttributeAttribute()]
InBlock.gif    
public string DomainName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.domainNameField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.domainNameField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlAttributeAttribute()]
InBlock.gif    
public string Remark
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.remarkField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.remarkField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Url#region Url
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Url索引器
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="name"></param>
ExpandedSubBlockEnd.gif    
/// <returns>返回相关的Url</returns>

InBlock.gif    public Url this[string name]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (Url url in urlField)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (url.Name.Equals(name))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return url;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <remarks/>
None.gif[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd""2.0.50727.42")]//可删除
None.gif
[System.SerializableAttribute()]
None.gif[System.Diagnostics.DebuggerStepThroughAttribute()]
//可删除
None.gif
[System.ComponentModel.DesignerCategoryAttribute("code")]//可删除
None.gif
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
None.gif
//修改前的生成代码
None.gif
//[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/CooperationChannels.xsd", IsNullable=false)]
None.gif
//修改后的代码
None.gif
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
None.gif
public partial class Url
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
private string nameField;
InBlock.gif
InBlock.gif    
private string baseTitleField;
InBlock.gif
InBlock.gif    
private string urlRemarkField;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlAttributeAttribute()]
InBlock.gif    
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.nameField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.nameField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlAttributeAttribute()]
InBlock.gif    
public string BaseTitle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.baseTitleField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.baseTitleField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
InBlock.gif    [System.Xml.Serialization.XmlAttributeAttribute()]
InBlock.gif    
public string UrlRemark
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.urlRemarkField;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.urlRemarkField = value;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

3.最后是调用的代码
ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
InBlock.gif        
/// 取得Channel,一开始是用反序化的方式进行做的,
InBlock.gif        
/// 但是最后因为性能的问题,所以改用loadxml的方式,
InBlock.gif        
/// 然后在xml数据放在动态cache中,进行使用的。
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   static  XmlNodeList GetChannel( string  path)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            XmlDocument xmlDoc 
= new XmlDocument();
InBlock.gif            XmlTextReader xmlTextReader 
= new XmlTextReader(path);
InBlock.gif            
InBlock.gif            xmlDoc.Load(xmlTextReader);
InBlock.gif            
return xmlDoc.ChildNodes;
ExpandedBlockEnd.gif        }

4.显示从xml中取得数据,并显示取出的数据。
ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 查找CooperationChannelsData.xml中对应的域名和标题并加载
ExpandedBlockEnd.gif        
/// </summary>

None.gif        private void GetRelatedDomainAndSetPageTitle()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//从StaticCache取到数据
InBlock.gif
            XmlNodeList channel = (XmlNodeList)CachingManagerStatic.GetData(CacheItem.CooperationChannel);
InBlock.gif
InBlock.gif            
if (channel == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlNodeList channelXml 
= CooperationChannelsHelp.GetChannel(GetXmlPath());
InBlock.gif
InBlock.gif                
//添加数据到StaticCache中
InBlock.gif
                CachingManagerStatic.Add(CacheItem.CooperationChannel, channelXml);
InBlock.gif                channel 
= (XmlNodeList)CachingManagerStatic.GetData(CacheItem.CooperationChannel);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
foreach (XmlNode xmlNode in channel[0].ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (XmlNode child in xmlNode.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (child.Attributes["Name"].Value.Equals(GetServerName()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DomainName 
= child.ParentNode.Attributes["DomainName"].Value.ToString();
InBlock.gif                        
this.Page.Title = child.Attributes["BaseTitle"].Value.ToString();
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值