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.


<rootttt>
 <content>
  <pre>
    <aa>ma</aa>

    <aa>ma</aa>





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

Solution 1

Hey,

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 :)
  Permalink  
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

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

Solution 2

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[^]
  Permalink  
v2

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