我写了一段代码,大家看看是否正确:/----------------
'首先获得QueryString的值,此值为xml文件物理地址
Dim strFile As String = (Request.QueryString("File")) '使用Xml DOM方式打开xml文件,装入内存
'首先定义文件流对象
Dim objDom As New XmlDocument()
Dim objFile As New FileStream(strFile, FileMode.Open)
objDom.Load(objFile) '打开文件
'定义写入的根节点
Dim nodRoot As XmlNode = objDom.DocumentElement("Family")
'开始写入元素
Try
Dim nodMember As XmlElement = objDom.CreateElement("Member")
nodMember.SetAttribute("CallTitle", Title.Text)
nodRoot.AppendChild(nodMember)
'再次定义根节点
nodRoot = objDom.DocumentElement("Member")
'写入Member的子节点
'建立名称
Dim nameElement As XmlElement = objDom.CreateElement("Name")
nameElement.Value = Name.Text
nodRoot.AppendChild(nameElement)
'建立出生
Dim birthElement As XmlElement = objDom.CreateElement("Birth")
birthElement.Value = Birth.Text
nodRoot.AppendChild(birthElement)
'建立政治面貌
Dim poilticElement As XmlElement = objDom.CreateElement("Poiltic")
poilticElement.Value = Poiltic.Text
nodRoot.AppendChild(poilticElement)
'建立职务
Dim dutyElement As XmlElement = objDom.CreateElement("Duty")
dutyElement.Value = Duty.Text
nodRoot.AppendChild(dutyElement) '保存文件
objDom.Save(strFile)
'关闭文件
objFile.Close()Catch objErr As Exception ShowMessage.Visible = True
ShowMessage.Text = "写入xml文件错误,文件地址:" & strFile & "错误内容:" & objErr.Message & objErr.Source
objFile.Close()
Exit SubEnd Try
/---------------------------
请大家看看有没有错误,谢谢!
'首先获得QueryString的值,此值为xml文件物理地址
Dim strFile As String = (Request.QueryString("File")) '使用Xml DOM方式打开xml文件,装入内存
'首先定义文件流对象
Dim objDom As New XmlDocument()
Dim objFile As New FileStream(strFile, FileMode.Open)
objDom.Load(objFile) '打开文件
'定义写入的根节点
Dim nodRoot As XmlNode = objDom.DocumentElement("Family")
'开始写入元素
Try
Dim nodMember As XmlElement = objDom.CreateElement("Member")
nodMember.SetAttribute("CallTitle", Title.Text)
nodRoot.AppendChild(nodMember)
'再次定义根节点
nodRoot = objDom.DocumentElement("Member")
'写入Member的子节点
'建立名称
Dim nameElement As XmlElement = objDom.CreateElement("Name")
nameElement.Value = Name.Text
nodRoot.AppendChild(nameElement)
'建立出生
Dim birthElement As XmlElement = objDom.CreateElement("Birth")
birthElement.Value = Birth.Text
nodRoot.AppendChild(birthElement)
'建立政治面貌
Dim poilticElement As XmlElement = objDom.CreateElement("Poiltic")
poilticElement.Value = Poiltic.Text
nodRoot.AppendChild(poilticElement)
'建立职务
Dim dutyElement As XmlElement = objDom.CreateElement("Duty")
dutyElement.Value = Duty.Text
nodRoot.AppendChild(dutyElement) '保存文件
objDom.Save(strFile)
'关闭文件
objFile.Close()Catch objErr As Exception ShowMessage.Visible = True
ShowMessage.Text = "写入xml文件错误,文件地址:" & strFile & "错误内容:" & objErr.Message & objErr.Source
objFile.Close()
Exit SubEnd Try
/---------------------------
请大家看看有没有错误,谢谢!
解决方案 »
- ASP.NET 缓存有效时间设置
- css 选择器 ID的问题
- 关于一个报警功能
- 如何使下载文件的对话框里显示和要下载的文件的类型和文件名一样
- 一个小小的改动就能搞定,请大大帮我看一下,来看的都有分
- 如何遍历页面或Table控件或Datagrid控件中某类控件如radiolist,textbox来获得它们的值?
- 在VS集成环境下编写Aspnet应用程序时出现的权限问题
- 【急】页面中某个控件有个属性是类类型,如何保持这个属性?(详见内文)
- 发布:使用SqlConnection和SqlTransaction的心得。一个类,一个例子。
- 太菜了,初学菜鸟求救!!!!
- 为什么页眉文本也被我赋值了
- 传参数问题
xmlDoc.LoadXml("<Root><Family></Family></Root>");
XmlNode node=xmlDoc.SelectSingleNode("/Root/Family");
node.InnerXml="<Member CallTitle=\"XXX\"><Name>XXX</Name><Birth>XXX</Birth><Poiltic>XXX</Poiltic><Duty>XXX</Duty></Member>";
txtOut.Text=xmlDoc.InnerXml.ToString();
//xmlDoc.Save("younamer.xml");
InsertKey.InnerText=ParameterName; //给属性key赋值
AppendParameterXmlNode.Attributes.Append(InsertKey); //将指定的属性插入集合
XmlAttribute InsertValue=FParameterXmlDoc.CreateAttribute("value"); //在结点中插入value的属性,用来记录年份
InsertValue.InnerText=ParameterValue; //给属性value赋值
AppendParameterXmlNode.Attributes.Append(InsertValue); //将指定的属性插入集合
ParameterNode.AppendChild(AppendParameterXmlNode); //将接点插入参数表中
该程序添加了1个接点“add”,该接点有2个属性“key”、“value”。
最后将这个接点添加到“AppendParameterXmlNode”接点下面。
http://expert.csdn.net/Expert/topic/1238/1238415.xml?temp=.3332636
我的笨办法是读取文件,从新生成同名的xml