using System;
using System.Xml;
namespace xmltest
{
/// <summary>
/// XmlTransoform 的摘要说明。
/// </summary>
public class XmlTransform
{
public XmlTransform()
{
}
public ITagControl CreateControl(string nsuri,string tag,string innerxml)
{
return new SimpleTagControl(innerxml);
}
public string Transform(string xml,string nsuri)
{
System.IO.StringReader sr = new System.IO.StringReader(xml);
XmlTextReader reader = new XmlTextReader(sr);
System.IO.StringWriter sw = new System.IO.StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting=System.Xml.Formatting.Indented;
reader.Read();
while(!reader.EOF)
{
if(reader.NodeType==XmlNodeType.Element&&reader.NamespaceURI==nsuri)
{
string localname=reader.LocalName;
ITagControl tc=CreateControl(nsuri,reader.LocalName,reader.ReadInnerXml());
writer.WriteRaw(tc.Render());
// reader.Read(); reader.ReadInnerXml 已经进行的一次read
}
else
{
if(reader.NodeType==XmlNodeType.Element)
{
bool isEmpty=false;
if(reader.IsEmptyElement)
{
isEmpty=true;
}
writer.WriteStartElement(reader.Name);
int count = reader.AttributeCount;
for(int i=0;i<count;i++)
{
reader.MoveToAttribute(i);
writer.WriteAttributeString(reader.Name,reader.Value);
}
if(isEmpty)
{
writer.WriteEndElement();
}
reader.Read();
}
else
{
writer.WriteNode(reader,true);
}
}
}
return sw.ToString();
}
}
}
-------------------------------------------------
using System;
using System.Xml;
namespace xmltest
{
/// <summary>
/// SimpleTagControl 的摘要说明。
/// </summary>
public class SimpleTagControl: ITagControl
{
public SimpleTagControl()
{
}
#region ITagControl 成员
public string Render()
{
if(members==null||members.Count==0)
return innerXml;
else
{
System.IO.StringWriter w=new System.IO.StringWriter();
XmlTextWriter xw=new XmlTextWriter(w);
foreach(object key in members.Keys)
{
xw.WriteStartElement(key.ToString());
xw.WriteString(members[key].ToString());
xw.WriteEndElement();
}
return w.ToString();
}
}
protected string innerXml;
public string InnerXml
{
get
{
return innerXml;
}
}
#endregion
/// <input>innerXml</input>
public SimpleTagControl(string innerXml)
{
init(innerXml);
}
private System.Collections.SortedList members;
private void createMember(string key,string value)
{
if(members==null)
{
members=new System.Collections.SortedList();
}
if(members.ContainsKey(key))
{
members.Add(key,value);
}
else
{
members[key]=value;
}
}
private void init(string innerXml)
{
this.innerXml=innerXml;
if(innerXml.StartsWith("<"))
{
System.IO.StringReader sr = new System.IO.StringReader(innerXml);
XmlTextReader reader = new XmlTextReader(sr);
while(reader.Read())
{
if(reader.NodeType==XmlNodeType.Text)
{
return;
}
if(reader.NodeType==XmlNodeType.Element)
{
createMember(reader.LocalName,reader.ReadInnerXml());
}
}
}
}
}
}
--------------------------------
[STAThread]
static void Main(string[] args)
{
Class1 c1=new Class1();
c1.transform();
Console.ReadLine(); }
public void transform()
{
string xml="<a xmlns:bb=\"http://ddd/\" xmlns:b='ss' key=\"keyname\"><bb:b a='c'><a>aaa</a></bb:b><a as='sdf'/><b:c key=\"dd\"><b xmlns=\"http://ddd1/\"><bb:h>hhh</bb:h></b></b:c><d>d1d1d1</d><e>eee</e></a>";
XmlTransform st=new XmlTransform();
Console.WriteLine(st.Transform(xml,@"http://ddd/"));
}
-----------------------------------------------------
欢迎就此问题进行评论,初步代码有待改进...
--fallseir.li 2005.4.23 0:36
本文地址:http://com.8s8s.com/it/it41107.htm