Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Thank you for reading my question.

thi is my xml file.


C#
<rootttt>
 <content>
  <pre>
    <aa>ma</aa>
    <bb>mb</bb>
    <cc>mc</cc>

<aa>ma</aa>
<bb>mb</bb>
<cc>mc</cc>





1: for the tag a, b, c, not always here, maybe missing.
2: for the tag pre, maybe many, but at least one.
3: modify, yes, contains, deleting, changing value.

how to read / modify this xml file in C#?

i tried somet solutions, but my C# is bad,,,i can't do it. could someone give me a sample code? thank you very much!
Posted
Updated 18-Apr-12 2:32am
v2

Hey,

C#
refer following code
XmlDocument doc = new XmlDocument();
doc.Load("path_to_input_file");
// Make changes to the document.
XmlTextWriter xtw = new XmlTextWriter("path_to_output_file", Encoding.UTF8);
doc.WriteContentTo(xtw);


Best Luck
Happy Coding :)
 
Share this answer
 
Comments
erlvde 18-Apr-12 8:37am    
thank you very much for helping me..but could you give me more information in detail? i am very fresh man when coding...and the format of xml file...i don't know what happend to it..

<root>
<aa>sdfsf
<bb>
<nn>
<nb>sfsdf
<nob>sdfsdf

<nn>
<nb>sfsdf
<nob>sdfsdf

XML
Hi,

Here a small example that uses the XmlSerializer to write data from a class into an Xml and reads it back:

using System;
using System.Xml.Serialization;
using System.IO;

namespace XmlDataSerializer
{
    // The data class containing two properties
    [Serializable()]
    public class Data
    {
        public String FirstProperty { get; set; }
        public int SecondProperty { get; set; }
    }


    // The test program
    class Program
    {

        static void Main(string[] args)
        {

            Data tx = new Data();
            tx.FirstProperty = "Hello Word";
            tx.SecondProperty = 4711;

            // Write to XML
            XmlSerializer writer = new XmlSerializer(typeof(Data));
            using (FileStream file = File.OpenWrite("data.xml"))
            {
                writer.Serialize(file, tx);
            }

            // Read from XML
            Data rx;

            XmlSerializer reader = new XmlSerializer(typeof(Data));
            using (FileStream input = File.OpenRead("data.xml"))
            {
                rx = reader.Deserialize(input) as Data;
            }

            Console.WriteLine("1st = <{0}, 2nd = <{1}>", rx.FirstProperty, rx.SecondProperty);
            Console.ReadLine();



        }
    }
}



The XML looks like that:

<?xml version="1.0"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstProperty>Hello Word</FirstProperty>
  <SecondProperty>4711</SecondProperty>
</Data>


You may refer this.

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

Reading an XML file using .NET[^]
 
Share this answer
 
v2

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