3

我想将以下 XML 反序列化为以下类型。如何正确映射状态?(它目前没有被映射并且在反序列化过程之后保持为空)

<?xml version="1.0" encoding="UTF-8"?>
<job:job-status xsi:schemaLocation="http://marklogic.com/xdmp/job-status job-status.xsd" xmlns:job="http://marklogic.com/xdmp/job-status" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <job:forest>
    <job:forest-name>FOREST2</job:forest-name>
    <job:forest-id>1168048654236455340</job:forest-id>
    <job:status>completed</job:status>
    <job:journal-archiving>false</job:journal-archiving>
  </job:forest>
</job:job-status>
[XmlRoot("job-status", Namespace = "http://marklogic.com/xdmp/job-status")]
public class DatabaseRestoreStatus
{
    [XmlElement("status")]
    public string Status { get; set; }
}
4

1 回答 1

4

使用DataContract Serializer对我有用。我还必须再创建一个类。

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace SandboxConoleApp
{

    internal class Program
    { 
        private static void Main(string[] args)
        {
            DatabaseRestoreStatus data = null;
            using (var stream = File.Open("test.xml",FileMode.Open))
            {
                var formatter = new DataContractSerializer(typeof(DatabaseRestoreStatus));
                data = (DatabaseRestoreStatus)formatter.ReadObject(stream);
            } 
        }
    }

    [DataContract(Name = "job-status", Namespace = "http://marklogic.com/xdmp/job-status")]
    public class DatabaseRestoreStatus
    {
        [DataMember(Name = "forest")]
        public Forest Forest { get; set; }
    }

    [DataContract(Name = "forest", Namespace = "http://marklogic.com/xdmp/job-status")]
    public class Forest
    {
        [DataMember(Name = "status")]
        public string Status { get; set; }
    }
}
于 2013-05-24T18:42:12.123 回答