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/"));