C#簡單操作XML
類檔案:
class OperatorXML
{
/// <summary>
/// 确定資源檔案路徑,Resource為自己建立的目錄
/// </summary>
private string filePath = @"..\..\Resources\XMLFile.xml";
/// <summary>
///
/// </summary>
private XDocument xDoc ;
/// <summary>
/// 當窗體加載時, 周遊所有節點
/// </summary>
public List<Desktop> LoadNode()
{
List<Desktop> desktop = new List<Desktop>();
try
{
this.xDoc = XDocument.Load(filePath);//加載xml檔案
XElement root = xDoc.Root; //擷取根節點
//周遊所有節點
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
desktop.Add(new Desktop(elem.Attribute("IpAddr").Value,elem.Attribute("Alias").Value, elem.Attribute("Pwd").Value,elem.Attribute("UserName").Value, elem.Attribute("Description").Value, new Guid(elem.Attribute("Id").Value)));
}
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
return desktop;
}
/// <summary>
/// 周遊所有節點, 擷取節點的别名
/// </summary>
/// <returns></returns>
public List<string> GetNodeName()
{
List<string> nodeName = new List<string>();
try
{
this.xDoc = XDocument.Load(filePath);//加載xml檔案
XElement root = xDoc.Root; //擷取根節點
//周遊所有節點
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
nodeName.Add(elem.Attribute("Alias").Value);
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
return nodeName;
}
/// <summary>
/// 根據指定的id擷取對應的節點屬性
/// </summary>
/// <param name="id">指定的id</param>
/// <returns></returns>
public Desktop GetDesktop(string id)
{
this.xDoc = XDocument.Load(filePath);//加載xml檔案
XElement root = xDoc.Root; //擷取根節點
Desktop desktop = null;
//周遊所有節點
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == id)
{
desktop = new Desktop();
desktop.IpAddr = elem.Attribute("IpAddr").Value;
desktop.Alias = elem.Attribute("Alias").Value;
desktop.UserName = elem.Attribute("UserName").Value;
desktop.Pwd = elem.Attribute("Pwd").Value;
desktop.Description = elem.Attribute("Description").Value;
desktop.Id = new Guid(elem.Attribute("Id").Value);
}
}
return desktop;
}
/// <summary>
/// 添加一個節點
/// </summary>
public void AddSave(Desktop desktop)
{
try
{
this.xDoc = XDocument.Load(this.filePath);
this.xDoc.Root.Add(new XElement("Desktop",
new XAttribute("IpAddr",desktop.IpAddr),
new XAttribute("UserName", desktop.UserName),
new XAttribute("Description", desktop.Description),
new XAttribute("Pwd",desktop.Pwd),
new XAttribute("Alias", desktop.Alias),
new XAttribute("Id", desktop.Id)
));
this.xDoc.Save(this.filePath);
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
/// <summary>
/// 根據ID來修改某個節點
/// </summary>
/// <param name="id"></param>
public void modifyNode(Desktop desktop)
{
this.xDoc = XDocument.Load(this.filePath);
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == desktop.Id.ToString())
{
elem.SetAttributeValue("IpAddr", desktop.IpAddr);
elem.SetAttributeValue("Alias", desktop.Alias);
elem.SetAttributeValue("UserName", desktop.UserName);
elem.SetAttributeValue("Pwd", desktop.Pwd);
elem.SetAttributeValue("Description", desktop.Description);
}
}
this.xDoc.Save(this.filePath);
}
/// <summary>
/// 删除一個節點并儲存
/// </summary>
/// <param name="desktop"></param>
public bool RemoveSave(string id)
{
bool isOK = false;
this.xDoc = XDocument.Load(this.filePath);
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == id)
{
elem.Remove();
this.xDoc.Save(this.filePath);
isOK = true;
break;
}
}
return isOK;
}
}
xml檔案示例(XMLFile.xml):
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Desktop IpAddr="host1" UserName="administrator" Description="獨立伺服器" Pwd="pwd" Alias="禮儀" Id="dfbc7b37-b5b3-4c36-a9c4-64302d824c34" />
<Desktop IpAddr="host2" UserName="administrator" Description="咖啡獨立伺服器" Pwd="pwsd" Alias="季卡" Id="80da53e6-5d84-4dc3-bb3f-90fbf0174e06" />
</Root>