none
How to edit all the values from xml file???

    Question

  • Hi all,

    I wanna create a method which receives the xml file name, then read it and change the values...

    I have different xml files, each one has different nodes, so I just want a simple method that reads any xml file and iterates through all the nodes, and change all the values.

    I've tried some code

    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(xmlFile);
                   
                    string var = "//"+xmlDoc.DocumentElement.Name;
                    XmlNodeList nodeList = xmlDoc.SelectNodes(var);
                    foreach(XmlNode node in nodeList)
                    {
                        foreach(XmlNode childNode in node.ChildNodes)
                        {
                            switch (childNode.NodeType)
                            {
                                case XmlNodeType.Element: 
                                    childNode.InnerText = nodeValue;
                                    break;
                                case XmlNodeType.Attribute:
                                    childNode.InnerText = nodeValue;
                                    break;
                            }
                        }   
                    }
                    

    But only works with xml like this:

    <value>
    	<value1></value1>	
    	<value2></value2>
    	...
    </value>

    And I need a dinamic method for iterating over xml like this:

    <value>
    	<value1>
    		<value1.1></value1.1>
    		<value1.2></value1.2>
    	</value1>	
    	<value2>
    		<value2.1>
    			<value2.1.1></value2.1.1>
    		</value2.1>
    	</value2>
    	...
    </value>




    • Edited by Berenice G Thursday, July 25, 2013 8:27 PM
    Thursday, July 25, 2013 8:19 PM

Answers

  • Hi Berenice,

    I Write a simple for you.Hope it can help you!

    using System.Xml;
    namespace RecurseXmlDocmentGetRootInfo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"E:\Exc\RecurseXmlDocmentGetRootInfo\RecurseXmlDocmentGetRootInfo\XMLFile1.xml";
                ChangeXmlText(path);
            }
            private static void ChangeXmlText(string path)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                RecurseXmlDocmentGetRootInfo(doc.DocumentElement, "nodeText");
                doc.Save(path);
            }
            public static void RecurseXmlDocmentGetRootInfo(XmlNode root, string nodeText)
            {
                if (root.HasChildNodes)
                    RecurseXmlDocmentGetRootInfo(root.FirstChild, nodeText);
                if (root.NextSibling != null)
                    RecurseXmlDocmentGetRootInfo(root.NextSibling, nodeText);
                if (root is XmlText && root.ParentNode != null)
                {
                    root.InnerText = nodeText;
                }
                else if (root is XmlElement && root.InnerXml == "")
                {
                    root.InnerText = nodeText;
                }
            }
        }
    }

    Regards,

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Saturday, July 27, 2013 5:42 AM

All replies

  • Hi Berenice,

    I Write a simple for you.Hope it can help you!

    using System.Xml;
    namespace RecurseXmlDocmentGetRootInfo
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"E:\Exc\RecurseXmlDocmentGetRootInfo\RecurseXmlDocmentGetRootInfo\XMLFile1.xml";
                ChangeXmlText(path);
            }
            private static void ChangeXmlText(string path)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                RecurseXmlDocmentGetRootInfo(doc.DocumentElement, "nodeText");
                doc.Save(path);
            }
            public static void RecurseXmlDocmentGetRootInfo(XmlNode root, string nodeText)
            {
                if (root.HasChildNodes)
                    RecurseXmlDocmentGetRootInfo(root.FirstChild, nodeText);
                if (root.NextSibling != null)
                    RecurseXmlDocmentGetRootInfo(root.NextSibling, nodeText);
                if (root is XmlText && root.ParentNode != null)
                {
                    root.InnerText = nodeText;
                }
                else if (root is XmlElement && root.InnerXml == "")
                {
                    root.InnerText = nodeText;
                }
            }
        }
    }

    Regards,

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Saturday, July 27, 2013 5:42 AM
  • Thank's a lot Buernuo,

    But I would like to insert the value in ascendant order, like this:

    <main_node>
      <node0>value0</node0>
      <node1>value1</node1>
      <node2>value2</node2>
      <node3>
      	<node3.1>
      		<node3.1.1>value3</node3.1.1>
    		<node3.1.2>value4</node3.1.1>
    	</node3.1>
      </node3>
      <node4>value5</node4>		
    </main_node>

    All this because I need to fill this XML file with data stored in array object

    I've tried something like this, but doesn´t work

    m = 0;
    
    public static void RecurseXmlDocmentGetRootInfo(XmlNode root, object[] nodeText, int i)
            {
                m = i;
                if (root.HasChildNodes)
                    RecurseXmlDocmentGetRootInfo(root.FirstChild, nodeText,m);
                if (root.NextSibling != null)
                    RecurseXmlDocmentGetRootInfo(root.NextSibling, nodeText,m);
                if (root is XmlText && root.ParentNode != null)
                {
                    root.InnerText = "Value" + m; //(string)nodeText[m].ToString()
                    m++;
                }
                else if (root is XmlElement && root.InnerXml == "")
                {
                    root.InnerText = "Value" + m; //(string)nodeText[m].ToString()
                    m++;
                }
    }

    Thanks

    Berenice

    Tuesday, July 30, 2013 4:32 PM
  • Hi Berenice,


    Welcome to the MSDN forum!


    First, you should use “<node3.1.2>Value4</node3.1.2>” instead of “<node3.1.2>Value4</node3.1.1>” in the XML Document.


    According to your description, you want to insert the value in ascendant order. I give a sample works on my side to you.


    The XML Document:

    <?xml version="1.0" encoding="utf-8"?>
    <main_node>
      <node0>Value0</node0>
      <node1>Value1</node1>
      <node2>Value2</node2>
      <node3>
        <node3.1>
          <node3.1.1>Value3</node3.1.1>
          <node3.1.2>Value4</node3.1.2>
        </node3.1>
      </node3>
      <node4>Value5</node4>
    </main_node>

    The sample code:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    namespace XmlUpdate
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"E:\Projects\WFA\XmlUpdate\XmlUpdate\XMLFile1.xml");
                RecurseXmlDocmentGetRootInfo(doc.DocumentElement, new object[] { }, m);
                doc.Save(@"E:\Projects\WFA\XmlUpdate\XmlUpdate\XMLFile1.xml");
            }
            static int m = 0;
            public static void RecurseXmlDocmentGetRootInfo(XmlNode root, object[] nodeText, int i)
            {
                m = i;
                if (root.HasChildNodes)
                    RecurseXmlDocmentGetRootInfo(root.FirstChild, nodeText, m);
                if (root.NextSibling != null)
                    RecurseXmlDocmentGetRootInfo(root.NextSibling, nodeText, m);
                if (root is XmlText && root.ParentNode != null)
                {
                    root.InnerText = "Value" + m; //(string)nodeText[m].ToString()
                    m++;
                }
                else if (root is XmlElement && root.InnerXml == "")
                {
                    root.InnerText = "Value" + m; //(string)nodeText[m].ToString()
                    m++;
                }
            }
        }
    }
     

    If you have any further questions, please feel free to let us know.


    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, August 05, 2013 6:26 AM