none
don't know how to get started - confused - way too many options

    Question

  • i have an xml file - no schema file - just want to move to a certain level of node and then change values for the child nodes. 
    how hard can it be? 

    well for me this has been an all day task.  where do i start, what all do i need? 
    which of the 500 different ways of moving around the xml file should i use
    which of the 500 path names or whatever does one use?




    Friday, July 10, 2009 9:57 PM

Answers

All replies

  • If you are working on .NET Framework 3.5 I would recommend using Linq to Xml. Here is a nice document (though a bit lengthy) that can get you started http://msdn.microsoft.com/en-us/library/bb308960.aspx
    If you cannot or don't want to use Linq to Xml you need to use XmlDocument. In that case you just need to
    1) Load the document
    2) navigate to appropriate node or attribute using XPath and SelectNodes or SelectSingleNode method
    3) change the value or the text of the node/attribute you selected in the previous step.

    If this is not clear enough just post your xml to the forum and show us what you need to achieve (e.g. what is the expected output) and I am sure someone will try helping you with this.

    Pawel
    Friday, July 10, 2009 10:13 PM
  • You did not mention a language or a .NET framework version ...

    But here is a simple example of how to do it using VB and .NET 3.5:

    http://msmvps.com/blogs/deborahk/archive/tags/Xml/default.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, July 10, 2009 10:31 PM
  • Hi,

    Check the following example to loop through the Xml

    Sample Xml (d:\emp.xml)
    <Employees>
      <Employee Name="John">
         <Dept>Tech</Dept>
         <Location>India</Location>
      </Employee>
      <Employee Name="Baskar">
         <Dept>HR</Dept>
         <Location>US</Location>
      </Employee>
      <Employee Name="Surya">
         <Dept>Aviation</Dept>
         <Location>India</Location>
      </Employee>
    </Employees>
    
    C# .Net Code to Loop XML Data:
    
    Using System.Xml;
    
    public class XMLLopping
    {
    
        private void AddEmpDetails()
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(@"d:\emp.xml");
           
         //Get the Root element 
           XmlElement elmRoot = doc.DocumentElement;
          
         //Code block to loop through all the Employee Elements and Add a new Child <Company> with each <Employee> Element Starts
         
        //Get All <Employee> Elements of <Employees> 
         XmlNodeList lsEmp = elmRoot.GetElementsByTagName("Employee");
    
         IEnumerator iEmp = lsEmp.GetEnumerator();
    
         While(iEmp.MoveNext())
         {
             XmlElement  elmEmp = (XmlElement) iEmp.Current;
             
             XmlElement elmCompany = doc.CreateElement("Company");
    
             elmCompany.InnerText ="Company1";
             
             //Append <Company> as a child element of <Employee>
             elmEmp.AppendChild(elmCompany);
         }
             //Code block to loop through all the Employee Elements and Add a new Child <Company> with each <Employee> Element Ends
       }
    
        private void ChangeEmployeeData()
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(@"d:\emp.xml");
           
         //Get the Root element 
           XmlElement elmRoot = doc.DocumentElement;
    
         //Code block to change Employee John's Dept Starts
         elmDept = elmRoot.SelectSingleNode("Employee[@Name='John']/Dept")
    
        elmDept.InnerText ="Training";
         //Code block to change Employee John's Dept Ends
    
       }

    baskaran r
    Friday, September 11, 2009 7:12 AM
  • Hi thanks for the info and answer!
    Wednesday, October 07, 2009 3:48 PM
  • Thanks for the information and reply.
    Wednesday, October 07, 2009 3:49 PM