xml文档中存在错误(2 2)xmlns =''没想到

时间:2015-05-07 08:35:56

标签: c# xml linq serialization xml-deserialization

我得到的错误是遍布stackoverflow一次又一次地回答,我在代码中尝试了一些更改,但无法删除错误。这是我用于序列化和反序列化的类。请看一下。

我不理解XMLroot,XML元素和命名空间等术语。所以请相应地回答,比如我应该给出什么名称空间,什么是XML根目录。

如果你可以编辑它,那就太好了:

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(){}

  }
}

我的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>

1 个答案:

答案 0 :(得分:0)

试试这个

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; }
    }
}