Solved

write to xml asp.net

Posted on 2009-06-30
10
384 Views
Last Modified: 2012-05-07
i want to delete content of existing xml file and then write new data to it. how do i do that in vb.net asp.net thanks.
0
Comment
Question by:StewSupport
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24746454
In order to manuplate XML file in ASP.net and VB.net.

Use XmlDocument Class.

you can find example at following link.

http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx

0
 
LVL 15

Expert Comment

by:jinal
ID: 24746469
0
 

Author Comment

by:StewSupport
ID: 24746471
and then read this out so you can create a listbox out of this.
<class name ="English" id= "class1">
   <studentinfo id="1">
        <studentname>Anderson</studentname>
        <studentid>1111</studentid>
   </studentinfo>
</class>
i will have multiple class and multiple studentinfo. can you guys help please. thanks.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:jinal
ID: 24746492
In which format you want to display this. Or you just want to read this.
0
 

Author Comment

by:StewSupport
ID: 24746862
well i just want to read the xml in the format i gave abve and then put that into a select list
like this

<select ....>
<option group=English>
<option value=studentid>Student name</option>
</select>
0
 

Author Comment

by:StewSupport
ID: 24746982
sorry i want to read and write(create) xml file
0
 
LVL 15

Accepted Solution

by:
jinal earned 500 total points
ID: 24747082
Dim doc As New XmlDocument
        doc.Load("XMLFile1.xml")

        Dim ele As XmlElement = doc.CreateElement("class")
        Dim class2 As XmlAttribute = doc.CreateAttribute("name")
        Dim id2 As XmlAttribute = doc.CreateAttribute("id")
        class2.Value = "class2"
        id2.Value = "2"
        ele.Attributes.Append(class2)
        ele.Attributes.Append(id2)
        doc.DocumentElement.AppendChild(ele)
        doc.Save("XmlFile1.xml")

XMLFile1.xml

<?xml version="1.0" encoding="utf-8" ?>
<Data>
  <class name ="English" id= "class1">
    <studentinfo id="1">
      <studentname>Anderson</studentname>
      <studentid>1111</studentid>
    </studentinfo>
  </class>  
</Data>
0
 

Author Comment

by:StewSupport
ID: 24747532
this is the format i have
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <optgroup label="Bakery">
    <option value="111">Chicken</option>
  </optgroup>
  <optgroup label="BBQ">
    <option value="222">Fried</option>
  </optgroup>
</catalog>
how do i read it so that i would have label in the select list and then value and text in item?

i have this and dont know where to go from there.
 For Each productnode In nodelist
            If productnode.Attributes.Count > 0 Then
                For Each productsubnode In productnode.ChildNodes
                    'add to child list box                    
                    ListBox_Section1.Items.Add(New ListItem(productsubnode.Name.ToString, productsubnode.Value.ToString))
                Next
            End If
        Next
0
 
LVL 15

Expert Comment

by:jinal
ID: 24749090
Dim groupNodes As XmlNodeList = doc.SelectNodes("/catalog/optgroup")
        Dim output As New System.Text.StringBuilder
        output.Append("<Select>")
        For Each node As XmlNode In groupNodes
            output.AppendFormat("<option group=""{0}"" />", node.Attributes(0).Value)
            Dim students As XmlNodeList = node.ChildNodes()
            For Each snode As XmlNode In students
                output.AppendFormat("<option value=""{0}"" >{1}</option>", snode.Attributes(0).Value, snode.InnerText)
            Next
        Next
        output.Append("<Select>")
0
 

Author Comment

by:StewSupport
ID: 24751163
the only thing i got out of your code is <select> <select>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question