Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

This is what I know to code..

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <product>
    <Product_ID>1</Product_ID>
    <Product_Name>product1</Product_Name>
    <product_Price>1111</product_Price>
  </product>
</NewDataSet>

But, I try to do like this...

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <product>
    <Product_ID>1</Product_ID>
    <Product_Name>product1</Product_Name>
    <product_Price>
        <Product_mode1>100</Product_mode1>
        <Product_mode2>150</Product_mode2>
        <Product_mod3e>200</Product_mode3>
    </product_Price>
  </product>
</NewDataSet>


How to add <product_price> like above..?
VB2010

What I have tried:

Dim myxml As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        myxml.Load(Application.StartupPath & "\Product.xml")
        Dim myxmlrecord As System.Xml.XmlElement = myxml.CreateElement("product")
        Dim myxmlfield As System.Xml.XmlElement

        myxmlfield = myxml.CreateElement("Product_ID")
        myxmlfield.InnerText = 1
        myxmlrecord.AppendChild(myxmlfield)
        myxmlfield = myxml.CreateElement("Product_Name")
        myxmlfield.InnerText = "product1"
    

        myxmlrecord.AppendChild(myxmlfield)
        myxmlfield = myxml.CreateElement("product_Price")
        myxmlfield.InnerText = 1111
        myxmlrecord.AppendChild(myxmlfield)
       
        myxml.ChildNodes(1).AppendChild(myxmlrecord)
        myxml.Save(Application.StartupPath & "\Product.xml")
Posted
Updated 11-Feb-20 22:10pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

myxmlfield = myxml.CreateElement("product_Price")

Dim myxmlmode = myxml.CreateElement("Product_mode1")
myxmlmode.InnerText = "100"
myxmlfield.AppendChild(myxmlmode)

myxmlmode = myxml.CreateElement("Product_mode2")
myxmlmode.InnerText = "150"
myxmlfield.AppendChild(myxmlmode)

'' etc...
   
v4
Comments
Member 13358124 12-Feb-20 3:51am
   
It shows ['CreateElement' is not a member of 'system.xml.xmlElement'] when type
Dim myxmlmode = myxmlfield.CreateElement("Product_mode1").
phil.o 12-Feb-20 3:58am
   
My bad; try myxml.CreateElement instead. I corrected the solution.
Member 13358124 12-Feb-20 4:19am
   
Thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100