简体   繁体   中英

there is an error in xml document (2 2) xmlns='' was not expected

The error I am getting is all over stackoverflow answered again and again, I have tried few changes in the code but not able to remove the error. here is the class I am using for serialization and deserialization. Please have a look at it.

I don't understand terms like XMLroot, XML element and namespace. So please answer accordingly, like what namespace should I give, what could be the XML root.

If u can edit it, it would be great:

namespace tudumo9
{

  public class data
  {
    public string project_name;
    public string note_text;
    public string tag_text;
    public DateTime start_date;
    public DateTime due_date;
    public string action;

    public  data(){}

  }
}

My XML:

<?xml version="1.0"?>
<ArrayOfData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <data>
    <project_name>p1</project_name>
    <tag_text>tagged</tag_text>
    <start_date>0001-01-01T00:00:00</start_date>
    <due_date>0001-01-01T00:00:00</due_date>
    <action>Action</action>
  </data>
</ArrayOfData>

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication25
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ArrayOfData));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            ArrayOfData newArrayOfData = (ArrayOfData)xs.Deserialize(reader);

        }

    }
    [XmlRoot("ArrayOfData")]
    public class ArrayOfData
    {
        [XmlElement("data")]
        public List<data> c_Data { get; set; }
    }

    [XmlRoot("data")]
    public class data
    {
        [XmlElement("project_name")]
        public string project_name { get; set; }
        [XmlElement("note_text")]
        public string note_text { get; set; }
        [XmlElement("tag_text")]
        public string tag_text { get; set; }
        [XmlElement("start_date")]
        public DateTime start_date { get; set; }
        [XmlElement("due_date")]
        public DateTime due_date { get; set; }
        [XmlElement("action")]
        public string action { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM