1. 新增XML文件
上述代码实现效果:
那在Person节点继续增加节点,实现也很简单
上述代码实现效果:
2.读取节点的值,个人感觉xpath方式比linq to xml更为明了方便
3.格式化显示XML
4.XMLToolV2源代码
public XMLToolV2(string xmlPath)
{
_xmlPath = xmlPath;
LoadXmlDoc();
}
public XmlNode Select(string xPath)
{
if (string.IsNullOrEmpty(xPath))
throw new ArgumentNullException("xPath");
return _xmlDoc.SelectSingleNode(xPath);
}
public XmlNodeList SelectNodeList(string xPath)
{
if (string.IsNullOrEmpty(xPath))
throw new ArgumentNullException("xPath");
return _xmlDoc.SelectNodes(xPath);
}
public void Create(string rootName, string encode)
{
CreateXmlDoc(rootName, encode);
}
private void CreateXmlDoc(string rootName, string encode)
{
if (!File.Exists(_xmlPath))
{
if (string.IsNullOrEmpty(rootName))
throw new ArgumentNullException(rootName);
_xmlDoc = new XmlDocument();
XmlDeclaration _xmldecl = _xmlDoc.CreateXmlDeclaration("1.0", encode, null);
_xmlDoc.AppendChild(_xmldecl);
_xmlRoot = _xmlDoc.CreateElement("", rootName, "");
_xmlDoc.AppendChild(_xmlRoot);
}
}
public void LoadXmlDoc()
{
if (File.Exists(_xmlPath))
{
_xmlDoc = new XmlDocument();
_xmlDoc.Load(_xmlPath);
_xmlRoot = _xmlDoc.DocumentElement;
}
}
public void Save()
{
if (_xmlDoc != null)
{
_xmlDoc.Save(_xmlPath);
}
}
public XmlElement CreateElec(string elecName, string elecValue)
{
XmlElement _xElec = CreateElement(_xmlRoot, elecName, elecValue);
return _xElec;
}
private XmlElement CreateElement(XmlNode _xmldocSelect, string elecName, string elecValue)
{
if (_xmldocSelect != null)
{
XmlElement _xElec = _xmlDoc.CreateElement(elecName);
_xElec.InnerText = elecValue;
_xmldocSelect.AppendChild(_xElec);
return _xElec;
}
return null;
}
public XmlElement CreateElec(XmlElement xmlParentElec, string elecName, string elecValue)
{
if (xmlParentElec != null)
{
XmlElement _xElec = CreateElement(xmlParentElec, elecName, elecValue);
return _xElec;
}
return null;
}
public void SetAttribute(XmlElement xElement, string attrName, string attrValue)
{
if (xElement != null)
{
xElement.SetAttribute(attrName, attrValue);
}
}
public int Check(string xpath)
{
if (string.IsNullOrEmpty(xpath))
throw new ArgumentNullException("xpath");
return SelectNodeList(xpath).Count;
}
public string ShowXml()
{
if (_xmlDoc != null)
{
return _xmlDoc.OuterXml;
}
return string.Empty;
}
public static string FormatXml(string xmlString, string encode)
{
if (string.IsNullOrEmpty(xmlString))
throw new ArgumentNullException("xmlString");
if (string.IsNullOrEmpty(encode))
throw new ArgumentNullException("encode");
MemoryStream _mstream = new MemoryStream();
XmlTextWriter _writer = new XmlTextWriter(_mstream, null);
XmlDocument _xDoc = new XmlDocument();
_writer.Formatting = Formatting.Indented;
_xDoc.LoadXml(xmlString);
_xDoc.WriteTo(_writer);
_writer.Flush();
_writer.Close();
Encoding _encoding = Encoding.GetEncoding(encode);
string _xmlString = _encoding.GetString(_mstream.ToArray());
_mstream.Close();
return _xmlString;
}
public static void Loop(XmlNodeList nodeList, Action<XmlNode> xmlNode)
{
if (nodeList != null)
{
foreach (XmlNode xNode in nodeList)
{
xmlNode(xNode);
}
}
}
}
新闻热点
疑难解答