求救!那位大哥能提供一份读写XML的公共类啊,小弟急用!在线等!

jiayong0316 2006-12-08 02:04:34
求救!那位大哥能提供一份读写XML的公共类啊,小弟急用!在线等!
...全文
150 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
myh0305 2006-12-10
  • 打赏
  • 举报
回复
XmlDocument方法或者XmlTextWriter,XmlTextReader
liujia_0421 2006-12-09
  • 打赏
  • 举报
回复
关于如何用C#操作Xml,给你一篇文章,基本的问题都能搞定..

http://www.blogcn.com/user63/rhfan2005/blog/25161802.html
liujia_0421 2006-12-09
  • 打赏
  • 举报
回复
到System.Xml里面找..
devilok 2006-12-09
  • 打赏
  • 举报
回复
public bool InsertToXML(string LocalIP,string UserSN,DateTime ExcDateTime,string ExceptionInfo)
{
try
{
string theDay = ExcDateTime.Year + "-" + ExcDateTime.Month + "-" + ExcDateTime.Day;
string FolderName = ConfigurationSettings.AppSettings["ExceptionFolder"];
string FileName = FolderName + "\\Exception_" + theDay + ".xml";
string InfoTitle = ""; //异常标题
string InfoAppName = ""; //程序名称
string TmpExcInfo = ""; //异常信息

#region 从ExceptionInfo中分析出异常标题、程序名称、异常信息

TmpExcInfo=ExceptionInfo;
InfoTitle = TmpExcInfo.Substring(0,TmpExcInfo.IndexOf(" at "));
TmpExcInfo=ExceptionInfo;
InfoAppName = TmpExcInfo.Substring(TmpExcInfo.LastIndexOf(" in ")+4,TmpExcInfo.Length-TmpExcInfo.LastIndexOf(" in ") - 5);
InfoAppName = InfoAppName.Substring(0,InfoAppName.LastIndexOf(":line"));
TmpExcInfo=ExceptionInfo;
#endregion

#region 创建文件夹、文件

if (Directory.Exists(FolderName) == false)
{ //若文件夹不存在,则创建
DirectoryInfo dinfo = Directory.CreateDirectory(FolderName);
}
if (File.Exists(FileName) == false)
{ //若文件不存在,则创建
StreamWriter wfile = File.CreateText(FileName);

wfile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
wfile.WriteLine("<ExceptionInformation>");
wfile.WriteLine(" <Exception DateTime=\"" + theDay + "\" Title=\"异常标题\">");
wfile.WriteLine(" <LocalIP>计算机IP</LocalIP>");
wfile.WriteLine(" <UserSN>操作人工号</UserSN>");
wfile.WriteLine(" <AppDomainName>程序名称</AppDomainName>");
wfile.WriteLine(" <Title>异常标题</Title>");
wfile.WriteLine(" <Information>异常详细信息</Information>");
wfile.WriteLine(" </Exception>");
wfile.WriteLine("</ExceptionInformation>");

wfile.Flush();
wfile.Close();
}
#endregion

#region 增加Exception

XmlDocument myXml = new XmlDocument();
myXml.Load(FileName);
XmlNode root = myXml.SelectSingleNode("ExceptionInformation");

XmlElement xel = myXml.CreateElement("Exception");
xel.SetAttribute("DateTime",ExcDateTime.ToString());
xel.SetAttribute("Title",InfoTitle);

XmlElement xelSub = myXml.CreateElement("LocalIP");
xelSub.InnerText = LocalIP;
xel.AppendChild(xelSub);
xelSub = myXml.CreateElement("UserSN");
xelSub.InnerText = UserSN;
xel.AppendChild(xelSub);
xelSub = myXml.CreateElement("AppDomainName");
xelSub.InnerText = InfoAppName;
xel.AppendChild(xelSub);
xelSub = myXml.CreateElement("Title");
xelSub.InnerText = InfoTitle;
xel.AppendChild(xelSub);
xelSub = myXml.CreateElement("Information");
xelSub.InnerText = TmpExcInfo;
xel.AppendChild(xelSub);

root.AppendChild(xel);
myXml.Save(FileName);
#endregion

return true;
}
}
scow 2006-12-08
  • 打赏
  • 举报
回复
盖茨大叔的xmldocument, xmltextreader, xmltextwriter就挺好的
cangwu_lee 2006-12-08
  • 打赏
  • 举报
回复
嗯 不错
icefeiji 2006-12-08
  • 打赏
  • 举报
回复
public class XmlControl
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();

public XmlControl(string XmlFile)
{
//
// TODO: 在這裡加入建構函式的程式碼
//
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}

public DataView GetData(string XmlPathNode)
{
//查找數據。返回一個DataView
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}

public void Replace(string XmlPathNode,string Content)
{
//更新節點內容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}

public void Delete(string Node)
{
//刪除一個節點。
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}

public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
{
//插入一節點和此節點的一子節點。
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
{
//插入一個節點,帶一屬性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Content)
{
//插入一個節點,不帶屬性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void Save()
{
//保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}

我帮你找的,不过我用了,还行.
KJ_Wang 2006-12-08
  • 打赏
  • 举报
回复
写个简单的结构吧.
public class InputOutputXML
{
public void InputFromXML(DataSet ds,string strPathAndFile)
{
ds.readXML(strPathAndFile);
}
public void OutputToXML(DataSet ds,string strPathAndFile)
{
ds.WriteXml(strPathAndFile);
}
}
这样好像可以.

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧