locked
Read xML not working RRS feed

  • Question

  •  

    <?xml version="1.0" encoding="utf-8" ?>
    <Tenders>
      <Ravina>
        <BillNo>1</BillNo>
        <PageNo>10</PageNo>
        <Activity>Metals</Activity>
      </Ravina>
      <Ravina>
        <BillNo>2</BillNo>
        <PageNo>20</PageNo>
        <Activity>Formworks</Activity>
      </Ravina>
      <Ravina>  
            <BillNo>3</BillNo>
      <PageNo>30</PageNo>
      <Activity>SiteWorks</Activity>
      </Ravina>
    </Tenders>

    var path = Server.MapPath(@"~/Product.xml");
            XmlTextReader xmlreader = new XmlTextReader(path);
            //reading the xml data  
            DataSet ds = new DataSet();
            ds.ReadXml(xmlreader);
            xmlreader.Close();

     XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNodeList xmlnode;
            int i = 0;
            string str = null;

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            xmldoc.Load(fs);
            xmlnode = xmldoc.GetElementsByTagName("Ravina");
            for (i = 0; i <= xmlnode.Count - 1; i++)
            {
                 str=   xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                str = xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
                //  str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + "  " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + "  " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                // MessageBox.Show(str);
            }

    write xml

     using (XmlWriter writer = XmlWriter.Create("C:\\ny\\myXmFile.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Employees");

                //foreach (Employee employee in employees)
                //{
                    writer.WriteStartElement("Employee");

                    writer.WriteElementString("ID","123".ToString());   // <-- These are new
                    writer.WriteElementString("FirstName", "mohit");
                    writer.WriteElementString("LastName", "tiwari");
                    writer.WriteElementString("Salary", "1500");

                    writer.WriteEndElement();
               // }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }


    • Edited by Mohtjwari Thursday, September 21, 2017 1:25 PM
    Tuesday, September 19, 2017 6:25 AM

All replies

  • Please validate your XML using XML Formatter and try again.
    Tuesday, September 19, 2017 7:32 AM
  • Hi Mohjwari,

    Thank you for posting here.

    For your question, what is the type of your project? The following code is used for asp.net. 

    var path = Server.MapPath(@"~/Product.xml");

    I test the code. No compile error. Could you provide more information about what you want?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 21, 2017 3:46 PM
  • Hi Mohjwari,

    Have you solved your problem?

    If you solved your problem, please mark the useful reply as answer. This will make answer searching in the forum and be beneficial to community members as well.


    If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, September 24, 2017 7:46 AM