Click here to Skip to main content
12,831,317 members (53,343 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XML
Thank you for reading my question.

thi is my xml file.



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 18-Apr-12 3:28am
Updated 18-Apr-12 3:32am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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

Best Luck
Happy Coding :)
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..



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

Solution 2


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
    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);


The XML looks like that:

<?xml version="1.0"?>
<Data xmlns:xsi="" xmlns:xsd="">
  <FirstProperty>Hello Word</FirstProperty>

You may refer this.[^]

Reading an XML file using .NET[^]

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 18 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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