读和写
string path="D:\\iSchoolCardStation.xml";
System.Xml.XmlDocument xd=new System.Xml.XmlDocument();
xd.Load(path);
System.Xml.XmlNode xn = xd.DocumentElement.SelectSingleNode("userSettings").SelectSingleNode("iSchoolCardStation.Properties.Settings").SelectSingleNode("luo");
//.SelectSingleNode("iSchoolCardStation.Properties.Settings");
if(xn!=null)
{
string x = xn.InnerText;
xn.InnerText = "emicetest";
xd.Save(path);
}
选择节点。
System.Xml.XmlNode xn = xd.DocumentElement.SelectSingleNode("userSettings").SelectSingleNode("iSchoolCardStation.Properties.Settings").SelectSingleNode("luo");
读出来一个子节点以后去掉这个自节点
XmlNode xn = xd.DocumentElement.SelectSingleNode("FileSingle");
int k = xn.ChildNodes.Count;
for (int i = 0; i < k; i++)
{
XmlNode xdd = xn.FirstChild;
XmlNodeList yy = doc.DocumentElement.ChildNodes; 有命名空间时候还能读子节点
xn.RemoveChild(xn.FirstChild);
}
//读取
public static string GetAttrbutesValue(string xml, string XmlNodeName, string Attributes)
{
try
{
XmlDocument doc = new XmlDocument();
doc.InnerXml = xml;
System.Xml.XmlNode xn = doc.SelectSingleNode(XmlNodeName);
string rst = xn.Attributes.GetNamedItem(Attributes).Value;
return rst;
}
catch
{
return "";
}
}
//筹建
ResultSetValue Value = null;
XmlNode ResultSet = doc.CreateNode("element", "ResultSet", "");
DetailObject.AppendChild(ResultSet);
CreateAttribute(ResultSet, "name", name);
XmlNode Schema = doc.CreateNode("element", "Schema", "");
ResultSet.AppendChild(Schema);
XmlNode data = doc.CreateNode("element", "data", "");
ResultSet.AppendChild(data);
XmlNode row = doc.CreateNode("element", "row", "");
data.AppendChild(row);
public XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
{
try
{
XmlDocument doc = node.OwnerDocument;
XmlAttribute attr = null;
attr = doc.CreateAttribute(attributeName);
attr.Value = value;
node.Attributes.SetNamedItem(attr);
return attr;
}
catch (Exception err)
{
string desc = err.Message;
return null;
}
}
XmlDeclaration Declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);
XmlNode ResultSet = doc.CreateNode("element", "联系人:联系人", "");
doc.AppendChild(ResultSet);
XmlNode row = doc.CreateNode("element", "联系人:姓名", "");
ResultSet.AppendChild(row).InnerText = "张三丰";