XML serialize错误:类型system.string可能不在此上下文中使用

时间:2015-05-27 08:39:49

标签: c# xml xml-parsing

我有一个工作类:

public partial class WORK
{
    Public WORK()
    {
        oId= new List<WORKID>();
    }
    // I have got WORKID obj in constructor & Get, Set variables...

    [System.Xml.Serialization.XmlElementAttribute("WORKID")]
    public List<WORKID> WORKID
    {
        get{}
        set{}
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class WORKID
{
    public string SERVICEORDERNO{}
    public string TECH{}
    public object APPOINTMENT{}
    //etc...
}

在大班......

WORID owork = new WORKID();
//assigned 

WORKID类及其相关类的所有值。

现在错误是序列化时的错误

var xmlserializer = new XmlSerializer(typeof(WORK));
using (var writer = new StreamWriter(@"C:\Users\user1\Desktop\out.xml"))
      {
          xmlserializer.Serialize(writer, owork);
      }

例外:

  

在此上下文中不能使用System.String类型。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是一份工作副本:

                var x = new WORK
                {
                    WORKID = new List<WORKID>
                    {
                        new WORKID
                        {
                            APPOINTMENT = "A",
                            SERVICEORDERNO = "!",
                            TECH = ".Net"
                        },
                        new WORKID
                        {
                            APPOINTMENT = "A",
                            SERVICEORDERNO = "!",
                            TECH = ".Net"
                        }
                    }
                };

                var xs = new XmlSerializer(typeof(WORK));
                using (var sw = new StreamWriter("c:\\path\\x.xml"))
                {
                    xs.Serialize(sw, x);
                }

注意我省略了所有Attributes