Solved

write to xml asp.net

Posted on 2009-06-30
10
383 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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