public class XmlConfig
{
private XmlDocument doc;
private string xmlFileName; public XmlConfig(string filename)
{
xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
//doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
}
} public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
} public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
} public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}      
node.InnerText = value;
}
}

//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}   
}

}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);

node.PrependChild(node3);
node3.InnerText=value;

}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{

XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}

public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
}

解决方案 »

  1.   

    XmlTextReader xmlReader = null;
    xmlReader = new XmlTextReader(mDocument); //mDocument参数为xml文档名字
    FileStream f_stream;
    f_stream = new FileStream(otherFileName, FileMode.CreateNew);
    BinaryWriter bWriter = new BinaryWriter(f_stream);while(xmlReader.Read())
    {
          if(xmlReader.NodeType == XmlNodeType.Element)
          {
      while(xmlReader.MoveToNextAttribute())
      {
            bWriter.Write(xmlReader.name);
        }
               if(xmlReader.IsEmptyElement)
      {
      }
      else
      {
                }
           }
           else if(xmlReader.NodeType == XmlNodeType.EndElement)
           {
      //做自己的处理
           }
           else if(xmlReader.NodeType == XmlNodeType.Text)
           {
       if (xmlReader.Value.Length != 0)
       {
                    bWriter.Write(xmlReader.Value);    }
           }
    }  //end of ~while(xmlReader.Read())希望有帮助!