1.根据XML的架构图,用xsd生成相应于的cs文件
命令为:xsd /c CooperationChannels.xsd
以下是CMD的结果图

C:\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd /c CooperationChannels.xsd
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Microsoft Visual Studio 8\SDK\v2.0\Bin\CooperationChannels.cs'.
XSD关系图:

相对应的xml文件

<?xml version="1.0" encoding="utf-8" ?>
<CooperationChannel>
<Channel DomainName="crp" Remark="XXX">
<Url Name="www.baidu.com" BaseTitle="" UrlRemark=""/>
<Url Name="mail.163.com" BaseTitle="" UrlRemark=""/>
<Url Name="XXXXX" BaseTitle="" UrlRemark=""/>
<Url Name="XXYYYY" BaseTitle="" UrlRemark="XXXX"/>
</Channel>
</CooperationChannel>
2.由于生成的文件,不能直接使用,所以需要对xsd生成的文件进行相关的修改
以下是修改的代码,注意注释


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

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]//可删除
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]//可删除
[System.ComponentModel.DesignerCategoryAttribute("code")]//可删除
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
//修改前的生成代码
//[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/CooperationChannels.xsd", IsNullable=false)]
//修改后的代码
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class CooperationChannel


{

//修改前的生成代码
//private Channel channelField;
//修改后的代码
private Channel[] channelField;


/**//// <summary>
/// 关系说明
/// XmlRootAttribute//根
/// XmlElementAttribute//节点
/// XmlAttributeAttribute//节点上的属性
/// </summary>
///需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlElementAttribute("Channel")]
//public Channel Channel //修改前的生成代码
public Channel[] Channel //修改后的代码

{
get

{
return this.channelField;
}
set

{
this.channelField = value;
}
}


Channel#region Channel


/**//// <summary>
/// 便于取值 Channel索引器
/// </summary>
/// <param name="_url"></param>
/// <returns>返回相关的Url</returns>
public Channel this[string _url]

{
get

{
foreach (Channel channel in channelField)

{
foreach (Url url in channel.Url)

{
if (url.Name.Equals(_url))

{
return channel;
}
}
}
return null;
}
}

#endregion
}


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


{
// private Url urlField; //修改前的生成代码
private Url[] urlField; //修改后的代码

private string domainNameField;

private string remarkField;

/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlElementAttribute("Url")]
//public Url Url //修改前的生成代码
public Url[] Url //修改后的代码

{
get

{
return this.urlField;
}
set

{
this.urlField = value;
}
}

/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DomainName

{
get

{
return this.domainNameField;
}
set

{
this.domainNameField = value;
}
}


/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Remark

{
get

{
return this.remarkField;
}
set

{
this.remarkField = value;
}
}


Url#region Url


/**//// <summary>
/// Url索引器
/// </summary>
/// <param name="name"></param>
/// <returns>返回相关的Url</returns>
public Url this[string name]

{
get

{
foreach (Url url in urlField)

{
if (url.Name.Equals(name))

{
return url;
}
}
return null;
}
}

#endregion
}


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


{

private string nameField;

private string baseTitleField;

private string urlRemarkField;


/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name

{
get

{
return this.nameField;
}
set

{
this.nameField = value;
}
}


/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlAttributeAttribute()]
public string BaseTitle

{
get

{
return this.baseTitleField;
}
set

{
this.baseTitleField = value;
}
}


/**////需要添加属性,[必须要添加的,不然无法确定xml中的关系]
[System.Xml.Serialization.XmlAttributeAttribute()]
public string UrlRemark

{
get

{
return this.urlRemarkField;
}
set

{
this.urlRemarkField = value;
}
}
}

3.最后是调用的代码

/**//// <summary>
/// 取得Channel,一开始是用反序化的方式进行做的,
/// 但是最后因为性能的问题,所以改用loadxml的方式,
/// 然后在xml数据放在动态cache中,进行使用的。
/// </summary>
public static XmlNodeList GetChannel(string path)

{
XmlDocument xmlDoc = new XmlDocument();
XmlTextReader xmlTextReader = new XmlTextReader(path);
xmlDoc.Load(xmlTextReader);
return xmlDoc.ChildNodes;
}
4.显示从xml中取得数据,并显示取出的数据。



/**//// <summary>
/// 查找CooperationChannelsData.xml中对应的域名和标题并加载
/// </summary>
private void GetRelatedDomainAndSetPageTitle()

{
//从StaticCache取到数据
XmlNodeList channel = (XmlNodeList)CachingManagerStatic.GetData(CacheItem.CooperationChannel);

if (channel == null)

{
XmlNodeList channelXml = CooperationChannelsHelp.GetChannel(GetXmlPath());

//添加数据到StaticCache中
CachingManagerStatic.Add(CacheItem.CooperationChannel, channelXml);
channel = (XmlNodeList)CachingManagerStatic.GetData(CacheItem.CooperationChannel);
}

foreach (XmlNode xmlNode in channel[0].ChildNodes)

{
foreach (XmlNode child in xmlNode.ChildNodes)

{
if (child.Attributes["Name"].Value.Equals(GetServerName()))

{
DomainName = child.ParentNode.Attributes["DomainName"].Value.ToString();
this.Page.Title = child.Attributes["BaseTitle"].Value.ToString();
break;
}
}
}