locked
XML read issue RRS feed

  • Question

  • User-102500655 posted

    not able to read xml back to data table

    here is the code i am creating my xml

     static void Main(string[] args)
            {
                string path = "";
                DataSet oDirectory = new DataSet();
                DirectoryDAL Dal = new DirectoryDAL();
    
                path = ConfigurationManager.AppSettings["path"];
    
                DataTable team = new DataTable();
                DataTable employees = new DataTable();
                DataTable teamemployees = new DataTable();
    
    
                team = Dal.LoadTeams();
                employees = Dal.LoadContacts();
                teamemployees = Dal.LoadTeamsContacts();
                XmlWriter xmlWriter = XmlWriter.Create(@path);
    
                xmlWriter.WriteStartDocument();
                xmlWriter.WriteStartElement("Directory");
    
    
                team.WriteXml(xmlWriter);
    
    
    
                employees.WriteXml(xmlWriter);
    
    
    
                teamemployees.WriteXml(xmlWriter);
    
    
                xmlWriter.WriteEndElement();
    
    
                xmlWriter.WriteEndDocument();
                xmlWriter.Close();
    
    
    
                //oDirectory.WriteXml(@"C:\XML\directory.xml");
    
            }

    now when i am trying to read it again it skip some nodes and not reading correctly.

    is there any solution??

    Saturday, January 19, 2013 11:04 AM

Answers

  • User3866881 posted

    Hi again,

    In fact you can use this XmlDocument to cope with your problem. I did this simple sample for you:

    class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                //Create the Root
                XmlElement root = doc.CreateElement("Root");
                //Create the sub node
                XmlElement item = doc.CreateElement("Teams");
                //Create nest team with value
                XmlElement sitem = doc.CreateElement("Team");
                sitem.InnerText = "My Value";
                item.AppendChild(sitem);
                root.AppendChild(item);
                doc.AppendChild(root);
                doc.Save("d:\\try.xml");
            }
          
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2013 1:09 AM

All replies

  • User3866881 posted

    Hi,

    If you have created a DataTable successfully, you can just use its WriteXml method to write into a file directly.

    Sunday, January 20, 2013 8:30 PM
  • User-102500655 posted

    abc

    Monday, January 21, 2013 1:00 AM
  • User3866881 posted

    Hi again,

    In fact you can use this XmlDocument to cope with your problem. I did this simple sample for you:

    class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                //Create the Root
                XmlElement root = doc.CreateElement("Root");
                //Create the sub node
                XmlElement item = doc.CreateElement("Teams");
                //Create nest team with value
                XmlElement sitem = doc.CreateElement("Team");
                sitem.InnerText = "My Value";
                item.AppendChild(sitem);
                root.AppendChild(item);
                doc.AppendChild(root);
                doc.Save("d:\\try.xml");
            }
          
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2013 1:09 AM