Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is what I know to code..

XML
<?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
<?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:

VB
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 21:10pm

1 solution

VB
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...
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900