Click here to Skip to main content
Rate this: bad
good
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>
    <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 18-Apr-12 2:28am
erlvde553
Edited 18-Apr-12 2: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 Smile | :)
  Permalink  
Comments
erlvde at 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..
 

sdfsf


sfsdf
sdfsdf


sfsdf
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
0 CHill60 330
1 OriginalGriff 285
2 Sergey Alexandrovich Kryukov 274
3 BillWoodruff 170
4 Aescleal 140


Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 18 Apr 2012
Copyright © CodeProject, 1999-2014
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