C# xml序列化

C# xml序列化,c#,xml-serialization,C#,Xml Serialization,在C语言中,如何解析或序列化xml(如下面的xml)thanx: <response> <result action="proceed" id="19809" status="complete" /> </response> 这将符合您的需要:或 正如@L.B所说,他是你在这件事上最大的朋友 其他解决方案: 为xml创建xsd模式。 使用xsd.exe为其创建类。 使用标准序列化进行序列化。 这就是我将助手类粘贴到项目中并序列化的时候 /// <

在C语言中,如何解析或序列化xml(如下面的xml)thanx:

<response>
<result action="proceed" id="19809" status="complete" />
</response>
这将符合您的需要:或 正如@L.B所说,他是你在这件事上最大的朋友

其他解决方案:

为xml创建xsd模式。 使用xsd.exe为其创建类。 使用标准序列化进行序列化。 这就是我将助手类粘贴到项目中并序列化的时候

    /// <summary>
/// Serialization helper
/// </summary>
public static class XmlSerializationHelper
{
    /// <summary>
    /// Deserializes an instance of T from the stringXml
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="xmlContents"></param>
    /// <returns></returns>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
    public static T Deserialize<T>(string xmlContents)
    {            
        // Create a serializer
        using (StringReader s = new StringReader(xmlContents))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(s);
        }
    }

    /// <summary>
    /// Serializes the object of type T to the filePath
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serializableObject"></param>
    /// <param name="filePath"></param>
    public static void Serialize<T>(T serializableObject, string filePath)
    {
        Serialize(serializableObject, filePath, null);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serializableObject"></param>
    /// <param name="filePath"></param>
    /// <param name="encoding"></param>
    public static void Serialize<T>(T serializableObject, string filePath, Encoding encoding)
    {
        // Create a new file stream
        using (FileStream fs = File.OpenWrite(filePath))
        {
            // Truncate the stream in case it was an existing file
            fs.SetLength(0);

            TextWriter writer; 
            // Create a new writer
            if (encoding != null)
            {
                writer = new StreamWriter(fs, encoding);
            }
            else
            {
                writer = new StreamWriter(fs);
            }   

            // Serialize the object to the writer
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(writer, serializableObject);

            // Create writer
            writer.Close();
        }
    }
}

只要把你的标题写在谷歌上,你就会得到很多好答案。