2015-01-07 2 views
1

C#에서 xml 문자열을 deseriaze하려면 다음 코드가 있습니다.C# deserializing 중첩 된 XML

모든 것이 잘 작동하며 개체에 대해 deserialize 할 수 있습니다. 그러나 ProjectNode 값은 항상 비어 있습니다.

누군가이 코드 작업을 도와 주거나 내가 누락 된 부분을 지적 할 수 있습니까?

샘플 XML은 아래 코드에 포함되었습니다.

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

namespace DeserializeSample 
{ 
    class Program 
    { 
     static string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><response clientos=\"Windows\" datetimepattern=\"M/d/yyyy h:mm:ss a\"><info><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><transaction loglevel=\"0\" type=\"response\"><arguments><argument name=\"id\">1</argument><argument name=\"foredit\">True</argument><argument name=\"ScheduleOn\">Estimated</argument><argument name=\"xml\"><Project xmlns=\"http://schemas.microsoft.com/project\"><UID>0</UID><ID>0</ID></Project></argument></arguments></transaction>]]></info></response>"; 

     static void Main(string[] args) 
     { 
      ProjectResponse projectResponse = CreateFromXml(XML, typeof(ProjectResponse)) as ProjectResponse; 
     } 

     public static object CreateFromXml(string data, Type msfRequestResponseType) 
     { 
      object projectResponse = null; 
      try 
      { 
       XmlSerializer deserializer = new XmlSerializer(msfRequestResponseType, ""); 
       XmlReaderSettings settings = new XmlReaderSettings() { ProhibitDtd = true }; 

       // We have content in the part so create xml reader and load the xml into XElement. 
       using (XmlReader reader = XmlReader.Create(new StringReader(data), settings)) 
       { 
        projectResponse = deserializer.Deserialize(reader); 
       } 
      } 
      catch (Exception Ex) 
      { 
       throw; 
      } 
      return projectResponse; 
     } 
    } 

    [XmlRoot("response")] 
    [Serializable] 
    public class ProjectResponse 
    { 
     [XmlAnyAttribute()] 
     public XmlAttribute[] ResponseAttributes { get; set; } 

     [XmlElement("info")] 
     public ProjectResponseInfoTag InfoTag { get; set; } 

     public class ProjectResponseInfoTag 
     { 
      private string infoText = string.Empty; 

      [XmlText] 
      public string InfoText 
      { 
       get { return infoText; } 
       set 
       { 
        infoText = value; 
        Transaction = Program.CreateFromXml(infoText, typeof(ProjectTransaction)) as ProjectTransaction; 
       } 
      } 

      [XmlElement("transaction")] 
      public ProjectTransaction Transaction { get; set; } 

      [XmlRoot("transaction")] 
      public class ProjectTransaction 
      { 
       [XmlAnyAttribute] 
       public XmlAttribute[] TransactionAttributes { get; set; } 

       [XmlElement("arguments")] 
       public ProjectArguments Arguments { get; set; } 

       public class ProjectArguments 
       { 
        [XmlElement("argument")] 
        public List<ProjectArgument> ArgList { get; set; } 

        public class ProjectArgument 
        { 
         [XmlAttribute("name")] 
         public string Name { get; set; } 

         [XmlText] 
         public string ArgValue { get; set; } 

         [XmlElement("Project")] 
         public Project ProjectNode { get; set; } 

         public class Project 
         { 
          [XmlAnyElement()] 
          public XmlElement[] ProjectElements { get; set; } 

          [XmlAnyAttribute()] 
          public XmlAttribute[] ProjectAttributes { get; set; } 
         } 
        } 
       } 
      } 
     } 
    } 

} 

답변

0

xml 네임 스페이스; 시도해보십시오.

[XmlElement("Project", Namespace="http://schemas.microsoft.com/project")] 
+0

완벽하게 작동합니다. 감사! – user2632730