Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sorry first, but I'm relay noob and I try to learn .

I have an Xml document like xmldoc.xml

<root>
    <one>first_one</one>
    <two>second_one</two>
    <tree>third_one</tree>
    <four>fourth_one</four>
    <five>fifth_one</five>
    <six>sixth_one</six>
</root>


What I have tried:

my problem is I try to read and/or update one or more elements inside my root node.
To be honest I don't have a clue how.

I try serializer but I stack because don't know to create class like I will try to explain:

C#
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmldoc.xml);
XmlNode root = xmldoc.FirstChild;

//create select_array[] = { one, tree four}
//create array new_val[] = {one => "first_second", tree => "tree_second", four => "four_second" }

// 1. create a class read_array values of one, tree four
// 2. create class save_array_new values of one, tree four

I don't know to create class read or save in this kind of way to can change one , two .. or all nodes values

up I write is only my idea to use array but i don't think is good
Posted
Updated 4-Feb-19 7:03am
Comments
[no name] 3-Feb-19 6:03am    
Here Solution 1 would probably help: read and update XML file using C#[^]

Here is a simple example using XElement:
            string TestXml = @"<root>
    <one>first_one</one>
    <two>second_one</two>
    <tree>third_one</tree>
    <four>fourth_one</four>
    <five>fifth_one</five>
    <six>sixth_one</six>
</root>";

            XElement x = XElement.Parse(TestXml);
            var one = x.Element("one");
            one.Value = "CHANGED ONE";
            x.Element("two").Remove();

            foreach (var item in x.Elements())
            {
                Debug.Print(item.ToString());
            }

If you prefer using XDocument, this is not much different, you will have to remember to use the root, see explanation here: Querying an XDocument vs. Querying an XElement (C#) | Microsoft Docs[^]
But as the MSDN documentation states:
Quote:
In many cases, your application will not require that you create a document. By using the XElement class, you can create an XML tree, add other XML trees to it, modify the XML tree, and save it.
XDocument Class Overview (C#) | Microsoft Docs[^]

Note that you only need XmlDocument when using .NET version 3.0 or lower.
 
Share this answer
 
v4
Comments
Maciej Los 3-Feb-19 12:14pm    
Well... OP wants to use XmlDocument class, instead of XDocument...
RickZeeland 3-Feb-19 12:53pm    
See the updated solution :)
Maciej Los 3-Feb-19 13:23pm    
Much better!
sz3bbylA 4-Feb-19 13:21pm    
Data at the root level is invalid. Line 1, position 1.
I not expert, srry, i don't know to use it
RickZeeland 4-Feb-19 14:17pm    
You have to place it inside a method, e.g. Main in Program.cs or any other method, good luck !
Note that this is an Winforms example that might not work with WPF.
Please, refer MSDN documentation: XmlDocument Class (System.Xml) | Microsoft Docs[^], especially Find nodes[^] and Edit nodes.
You might be interested in Select Nodes Using XPath Navigation[^]

[EDIT]
According to the OP requirements (posted in solution 3):
Quote:
I Want to keep the xml file like it is
1. The only think I need is to create class like read one, two, tree or all values elements
2. one class to update one, two, tree or all values elements


I'd recommend to change your xml structure to this one:
HTML
<MyItems>
    <MyItem id = "first">first one</Item>
    <MyItem id = "second">first one</Item>
    <MyItem id = "third">first one</Item>
    <MyItem id = "fourth">first one</Item>
    <!-- and so on -->
</MyItems>


A class representing single item this may look like:
C#
public class MyItem
{
	public string id = "first";
	public string text = "first_one";  
}


Finally, you need a collection of MyItem.

For further details, please read my tip: A Complete Sample of Custom Class Collection Serialization and Deserialization[^]. True, it's written in Vb.net, but you have to handle it, because Vb.net is pretty similar to C#.
 
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