怎么把从数据源的数据写入XML? 用Linq,XDocument和XElement可以实现,不过有些麻烦,简单一句代码的方法我也不知道 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 XmlDocument xmlDoc = new XmlDocument();try{ xmlDoc.Load(xmlFileName); //加载XML文档 XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); //xpath节点 //Get the root node XmlNode RootElem = xmlDoc.DocumentElement; //Get lastest update record XmlNode TempElem = RootElem.LastChild; string id = TempElem.Attributes["id"].Value; int userId = Int32.Parse(id) + 1; if (xmlNode != null) { XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); //如果属性和值参数都不为空则在此新节点上新增属性 if (!string.IsNullOrEmpty(pwd) && !string.IsNullOrEmpty(name)) { XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("name"); xmlAttribute.Value = name; XmlAttribute xmlAttribute1 = xmlDoc.CreateAttribute("pwd"); xmlAttribute1.Value = pwd; XmlAttribute xmlAttribute2 = xmlDoc.CreateAttribute("id"); xmlAttribute3.Value = userId.ToString(); subElement.Attributes.Append(xmlAttribute); subElement.Attributes.Append(xmlAttribute1); subElement.Attributes.Append(xmlAttribute2); } xmlNode.AppendChild(subElement); } xmlDoc.Save(xmlFileName); //保存到XML文档} 为何无法保存数据呢? 控件上绘制图片问题 C#中如何调用托管dll类中的方法? 数据集中DataTable与从数据库拖过来的表不一致 对象序列化如何控制xml格式? 如何反射ascx Web用户控件 汉字如何转拼音(请教) 如何通过鼠标点击网址,让这个网址传入我写得浏览器里面。。。 c# 请教:如何防止同一C#程序同时运行! 用C#读取文件中的数据 ckeditor+ckfinder
try
{
xmlDoc.Load(xmlFileName); //加载XML文档
XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath); //xpath节点
//Get the root node
XmlNode RootElem = xmlDoc.DocumentElement;
//Get lastest update record
XmlNode TempElem = RootElem.LastChild;
string id = TempElem.Attributes["id"].Value;
int userId = Int32.Parse(id) + 1;
if (xmlNode != null)
{
XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);
//如果属性和值参数都不为空则在此新节点上新增属性
if (!string.IsNullOrEmpty(pwd) && !string.IsNullOrEmpty(name))
{
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("name");
xmlAttribute.Value = name;
XmlAttribute xmlAttribute1 = xmlDoc.CreateAttribute("pwd");
xmlAttribute1.Value = pwd;
XmlAttribute xmlAttribute2 = xmlDoc.CreateAttribute("id");
xmlAttribute3.Value = userId.ToString();
subElement.Attributes.Append(xmlAttribute);
subElement.Attributes.Append(xmlAttribute1);
subElement.Attributes.Append(xmlAttribute2);
} xmlNode.AppendChild(subElement);
}
xmlDoc.Save(xmlFileName); //保存到XML文档
}