如何将子节点附加到现有的xml文件 [英] how to append child node to an existing xml file

查看:95
本文介绍了如何将子节点附加到现有的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我创建xmlfile的代码
当我第一次写入它时,它工作正常.当我再次插入内容时,它显示错误,仅允许一个根节点.
问题是当我再次添加节点时,我需要将其作为子节点附加到根节点,但是我不知道该怎么做.
请帮我做这个

This is my code to create xmlfile
when i''m writing into it for first time it''s working fine.when i''m inserting the content again it showing error only one root node is allowed.
The problem is when i''m additing nodes again i need to append it as a child node to the root node but i don''t know how to do it.
Please help me to do this

Sub CreateXmlFile()
       Dim objReportNodes As New System.Text.StringBuilder()
       Dim strDetails As String
       strDate = Request.Form("tBDate").ToString

       strTime = Request.Form("lBTime")

       strSubject = Request.Form("txtsubject")
       strAjanta = Request.Form("txtAjanta")
       strDetails = "<?xml version='1.0' encoding='windows-1252' ?>"
       strDetails &= "<Messages><Date>" & strDate & "</Date>" & _
                     "<Time>" & strTime & "</Time>" & _
                     "<Subject>" & strSubject & "</Subject>" & _
                     "<Ajanta>" & strAjanta & "</Ajanta>" & _
                     "</Messages>"
       objReportNodes.Append(strDetails)

       Dim objFile As System.IO.TextWriter = System.IO.File.AppendText("D:\XMLFile3.xml")

       objFile.WriteLine(objReportNodes)
       objFile.Close()
       objFile = Nothing
       objReportNodes = Nothing
       Call ReadXmlFile()
   End Sub



我尝试使用AppendChildNode(),但是在这里我正在使用文本编写器,因此它不会转换为system.xml.xmlnode



i tried using AppendChildNode() but here i''m using textwriter so it is not converting to system.xml.xmlnode

推荐答案

在构建XML时在字符串中,您可以只使用

As you''re constructing your XML in a string, you can just use

objReportNodes.LoadXml(strDetails)


工作解决方案


Working solution

Dim objReportNodes As New Xml.XmlDocument
Dim strDetails As String

Dim strDate As Date ' = Request.Form("tBDate").ToString
Dim strTime As Date ' = Request.Form("lBTime")

Dim strSubject As String '= Request.Form("txtsubject")
Dim strAjanta As String ' = Request.Form("txtAjanta")
strDetails = "<Message><Date>" & strDate & "</Date>" & _
              "<Time>" & strTime & "</Time>" & _
              "<Subject>" & strSubject & "</Subject>" & _
              "<Ajanta>" & strAjanta & "</Ajanta>" & _
              "</Message>"

If IO.File.Exists("XMLFile3.xml") Then objReportNodes.Load("XMLFile3.xml") Else objReportNodes.LoadXml("<Messages></Messages>")
objReportNodes.FirstChild.InnerXml &= strDetails

objReportNodes.Save("XMLFile3.xml")


首先,您在xml格式方面犯了一个错误.
xml的输出如下所示.

First a fall, you have a mistake in xml formation.
The output of your xml looks like this.

<messages>
    <date>strDate</date>
    <time>strTime</time>
    <subject>strSubject</subject>
    <ajanta>strAjanta</ajanta>
</messages>



此处的xml将考虑



Here xml will consider

<message></message>

作为父节点,如果您再插入一条记录,则会再次创建同一父节点,因此无法创建xml.

为此,您需要有一个父级,并且在该父级内部,您的xml格式应如下所示,

as the parent node and if u inserted one more record, the same parent node is again created,so xml cannot create.

For this you need to have one parent and inside that sub- parent, your xml formation should be like this,

<all-message>
<messages id="0">
    <date>strDate</date>
    <time>strTime</time>
    <subject>strSubject</subject>
    <ajanta>strAjanta</ajanta>
</messages>
<messages id="1">
    <date>strDate1</date>
    <time>strTime1</time>
    <subject>strSubject1</subject>
    <ajanta>strAjanta1</ajanta>
</messages>
</all-message>



如果您有自己的



if you have your own unique id for

<message></message>

很有用,以防您仅打印所需的< message>.



希望此链接对您的问题有所帮助:
http://msdn.microsoft.com/en-us/library/5x8bxy86 (v = vs.80).aspx [

it will be useful,incase you printing only that required <message>.



Hope this link will help for your question:
http://msdn.microsoft.com/en-us/library/5x8bxy86(v=vs.80).aspx[^]


这篇关于如何将子节点附加到现有的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆