dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] how to add element to specific position?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Location
    Jakarta,Indonesia
    Posts
    100

    Resolved [RESOLVED] how to add element to specific position?

    hi all,i have a xml like this
    <?xml version="1.0" encoding="UTF-16" standalone="no"?>
    <zylab>
    <document version="1.1" guid="{5BB6C5FF-FDA4-4FC5-99A2-20CFDF5651FE}" date="20060928" time="00:13:12.017" size="35595" path="E:\ZYIMAGE\INDEX DATA\PBI\TXT\2006\00000000\" name="200001TR.TXT" key="">
    <fields>
    <field id="Peraturan">6/6/PBI/2004</field>
    <field id="Tanggal">20040216</field>

    <field id="Perihal">Fasilitas Likuiditas Intrahari Bagi Bank Umum</field>
    <field id="Status">Mencabut : PBI No.2/26/PBI/2000 Tgl.13-12-2000; Dicabut oleh PBI No.7/22/PBI/2005 Tgl.03-8-2005</field>
    </fields>
    </document>
    </zylab>
    if i want to add some element like <field id="Satker"></field> to specific position (below tag <field id="Tanggal">). how i can be??
    i am trying in my code,but don't get good result. because the tag <field id="Satker"></field> always below tag <field id="Status">,(i am trying nextsibilng,childnodes,but don't get result.)

    this is my code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Dim xmldoc As New XmlDocument
    xmldoc.Load("C:\Data SARAH\contoh xml\{5BB6C5FF-FDA4-4FC5-99A2-20CFDF5651FE}.xml")

    'latihan nambah data di xml
    Dim NewElement As XmlNode = xmldoc.CreateNode(XmlNodeType.Element, "field", String.Empty)
    Dim NewAttribute As XmlAttribute = xmldoc.CreateAttribute("id")
    NewAttribute.Value = "Satker"

    NewElement.InnerText = ""
    NewElement.Attributes.Append(NewAttribute)

    Dim root As XmlElement = xmldoc.DocumentElement.FirstChild
    root.FirstChild.AppendChild(NewElement)
    xmldoc.Save("{5BB6C5FF-FDA4-4FC5-99A2-20CFDF5651FE}.xml")
    End Sub
    what i should add in my code?
    any one have a clue or key word so i can research in uncle google?

  2. #2
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: how to add element to specific position?

    Maybe something like this?
    Code:
            Dim xEleList As XmlNodeList = xmldoc.GetElementsByTagName("Tanggal")
            xmldoc.InsertAfter(NewElement, xEleList.Item(0))

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Location
    Jakarta,Indonesia
    Posts
    100

    Re: how to add element to specific position?

    Wow Thanks A LOT!!!
    It success!!
    thank you very much ZenDisaster for your help!

  4. #4
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: how to add element to specific position?

    My pleasure

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width