2

我对在我的文件中使用 XML 有点陌生,并且想要序列化然后反序列化一个对象。该对象包含一对字符串、一个 int 和两个 int[] 数组。使用 XmlSerializer 进行序列化,生成的 XML 看起来像这样:

http://codepaste.net/x9x5ms

<?xml version="1.0" encoding="utf-16"?>
<Harvey xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Carl</Name>
  <Ch>KNV</Ch>
  <Tn>40</Tn>
  <APoints>
    <int>8</int>
    <int>20</int>
    <int>16</int>
    <int>16</int>
    <int>12</int>
    <int>12</int>
    <int>16</int>
    <int>16</int>
    <int>4</int>
    <int>4</int>
    <int>4</int>
  </APoints>
  <SPoints>
    <int>3</int>
    <int>12</int>
    <int>10</int>
    <int>10</int>
    <int>6</int>
    <int>6</int>
    <int>10</int>
    <int>10</int>
  </SPoints>
</Harvey>

问题是试图将 int[] 值加载回它们的数组中。我无法弄清楚如何告诉应用程序我想将所有 Apoint 加载到 int[] 数组中。我怀疑解决方案非常简单,但我一直无法弄清楚。

4

3 回答 3

8

一样容易

[XmlRoot( "Harvey" )]
public class Widget
{

    [XmlElement]
    public string Name { get ; set; }

    [XmlElement]
    public string Ch   { get ; set; }

    [XmlElement]
    public int    Tn   { get ; set; }

    [XmlArray("APoints")]
    [XmlArrayItem("int")]
    public int[] APoints { get ; set ; }

    [XmlArray("SPoints")]
    [XmlArrayItem("int")]
    public int[] SPoints { get ; set ; }

}

class Program
{
    public static T Rehydrate<T>( string xml )
    {
        T instance ;
        XmlSerializer serializer = new XmlSerializer( typeof(T) ) ;

        using ( TextReader tr = new StringReader( xml ) )
        {
            instance = (T) serializer.Deserialize( tr ) ;
        }
        return instance ;
    }


    static void Main( string[] args )
    {
        string xml = @"
<Harvey xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <Name>Carl</Name>
  <Ch>KNV</Ch>
  <Tn>40</Tn>
  <APoints>
    <int>8</int>
    <int>20</int>
    <int>16</int>
    <int>16</int>
    <int>12</int>
    <int>12</int>
    <int>16</int>
    <int>16</int>
    <int>4</int>
    <int>4</int>
    <int>4</int>
  </APoints>
  <SPoints>
    <int>3</int>
    <int>12</int>
    <int>10</int>
    <int>10</int>
    <int>6</int>
    <int>6</int>
    <int>10</int>
    <int>10</int>
  </SPoints>
</Harvey>
";
        Widget instance = Rehydrate<Widget>( xml ) ;

        return;
    }

}
于 2012-08-18T00:00:16.940 回答
1

比序列化更简单的东西

XmlReader reader = XmlReader.Create(new StringReader(xml));
XElement root = XElement.Load(reader);

int[] myInts = ((IEnumerable)root.XPathEvaluate("APoints/int"))
                 .OfType<XElement>()
                 .Select(el => int.Parse(el.Value))
                 .ToArray();
于 2012-08-18T16:14:46.200 回答
-1

这是我前段时间写的课程:

public static class Serializer
{
    // returns false in case of an error
    public static bool save(string fileName, Object obj)
    {
        XmlSerializer serializer = null;
        TextWriter textWriter = null;
        bool res = false;

        try
        {
            serializer = new XmlSerializer(obj.GetType());
            textWriter = new StreamWriter(fileName);
            serializer.Serialize(textWriter,obj);
            res = true;
        }
        catch(Exception ex)
        {
            // handle error
        }
        finally
        {
            if (null != textWriter)
                textWriter.Close();
        }

        return res;
    }

    // returns null in case of an error
    public static Object load(Type type, string fileName)
    {
        XmlSerializer deserializer=null;
        TextReader textReader=null;
        Object res = null;
        try
        {
            deserializer = new XmlSerializer(type);
            textReader = new StreamReader(fileName);
            res=deserializer.Deserialize(textReader);

        }
        catch(Exception ex)
        {
            // handle error
        }
        finally
        {
            if (null != textReader)
                textReader.Close();
        }

        return res;
    }
}

这就是你使用它的方式:

public class Data
{
    public int a;
    public int[] arr;
}

private void button1_Click(object sender, EventArgs e)
{
    Data myData = new Data();
    myData.a = 2;
    myData.arr=new int[]{1,2,3,7};
    Serializer.save("file.xml", myData);
}

private void button2_Click(object sender, EventArgs e)
{
    Data myData;
    myData = (Data)Serializer.load(typeof(Data), "file.xml");

}
于 2012-08-17T23:55:44.803 回答