标签:exist single amp ring bool var tee create config
private string filePath = string.Empty;
private XmlDocument xmlDoc = new XmlDocument();
public XmlHelper(string _filePath)
{
filePath = _filePath;
if (File.Exists(filePath))
{
xmlDoc.Load(filePath);
}
else
{
xmlDoc = new XmlDocument();
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
}
}
public void ReadRootXml()
{
XmlNodeList xmlNodeList = xmlDoc.ChildNodes;
for (int i = 0; i < xmlNodeList.Count; i++)
{
XmlNode node = xmlNodeList[i];
if (node.NodeType == XmlNodeType.Element && node.Name == "Root")
{
XmlNodeList itemXmlNodeList = node.FirstChild.ChildNodes;
foreach (var item in itemXmlNodeList)
{
XmlNode itemNode = (XmlNode)item;
if (itemNode != null)
{
Config config = new Config();
XmlAttributeCollection attributeCollection = itemNode.Attributes;
for (int j = 0; j < attributeCollection.Count; j++)
{
XmlAttribute attribute = attributeCollection[j];
if (attribute.Name == "Name")
{
config.Name = attribute.Value;
}
else if (attribute.Name == "name")
{
config.name = attribute.Value;
}
else if (attribute.Name == "path")
{
config.path = attribute.Value;
}
else if (attribute.Name == "filePath")
{
config.filePath = attribute.Value;
}
}
fileConfigs.Add(config);
}
}
}
}
return fileConfigs;
}
private XmlNodeList GetXmlNodeList()
{
XmlNodeList xmlNodeList = xmlDoc.ChildNodes;
for (int i = 0; i < xmlNodeList.Count; i++)
{
XmlNode node = xmlNodeList[i];
if (node.NodeType == XmlNodeType.Element && node.Name == "Root")
{
XmlNodeList nodeList = node.FirstChild.ChildNodes;
return nodeList;
}
}
return null;
}
public bool Add(string pName, string name, string path, string filePath)
{
if (File.Exists(filePath))
{
if (FindElement(path) == null)
{
XmlNode xmlNode = xmlDoc.SelectSingleNode("descendant::Project");
xmlNode.AppendChild(AddXmlElement(pName, name, path, filePath, xmlDoc));
xmlDoc.Save(filePath);
}
}
else
{
XmlElement xmlElement = CreateXmlElement(pName, name, path, filePath, xmlDoc);
xmlDoc.AppendChild(xmlElement);
xmlDoc.Save(filePath);
}
return true;
}
private XmlElement AddXmlElement(string pName, string name, string path, string filePath, XmlDocument doc)
{
XmlElement content = doc.CreateElement("Item");
content.SetAttribute("ProjectName", pName);
content.SetAttribute("name", name);
content.SetAttribute("path", path);
content.SetAttribute("filePath", filePath);
return content;
}
private XmlElement CreateXmlElement(string pName, string name, string path, string filePath, XmlDocument doc)
{
XmlElement xmlElement = doc.CreateElement("Root");
doc.AppendChild(xmlElement);
XmlElement node = doc.CreateElement("Project");
XmlElement content = doc.CreateElement("Item");
content.SetAttribute("ProjectName", pName);
content.SetAttribute("name", name);
content.SetAttribute("path", path);
content.SetAttribute("filePath", filePath);
node.AppendChild(content);
xmlElement.AppendChild(node);
return xmlElement;
}
public void RemoveElement(string path)
{
XmlNode itemNode = FindElement(path);
if (itemNode != null)
{
XmlNode projectNode = itemNode.ParentNode;
projectNode.RemoveChild(itemNode);
xmlDoc.Save(filePath);
}
}
private XmlNode FindElement(string path)
{
XmlNode xmlNode = xmlDoc.SelectSingleNode("descendant::Item[@path = ‘" + path + "‘]");
return xmlNode;
}
标签:exist single amp ring bool var tee create config
原文地址:https://www.cnblogs.com/hopecode/p/11509408.html