2011-03-19 4 views
3
내가/역 직렬화 아래의 XML 파일 직렬화 할 필요가

포함하는 XML :문제/역 직렬화 CDATA 속성

<items att1="val"> 
<item att1="image1.jpg"> 
     <![CDATA[<strong>Image 1</strong>]]> 
</item> 
<item att1="image2.jpg"> 
     <![CDATA[<strong>Image 2</strong>]]> 
</item>  
</items> 

내 C# 클래스 :

[Serializable] 
[XmlRoot("items")]  
public class RootClass 
{ 
    [XmlAttribute("att1")] 
    public string Att1 {set; get;} 

    [XmlElement("item")] 
    public Item[] ArrayOfItem {get; set;} 
} 

    [Serializable] 
public class Item 
{ 
    [XmlAttribute("att1")] 
    public string Att1 { get; set; } 

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

를 모든 것이 거의 완벽한하지만 직렬화 후 작동 장소

<![CDATA[<strong>Image 1</strong>]]> 

에 나는

&lt;strong&gt;Image 1&lt;/strong&gt; 

콘텐츠 속성의 형식으로 XmlCDataSection을 사용하려고했지만이 형식은 XmlText 특성과 함께 사용할 수 없습니다. 불행히도 XML 구조를 변경할 수 없습니다.

이 문제를 어떻게 해결할 수 있습니까?

+1

'이미지 1]>'와 '< 강한 > 이미지 1 </강한 >'똑같은이다. 문제가 어디 있습니까? – Tomalak

+0

xml을 읽는 또 다른 응용 프로그램은 '<strong> Image 1 </strong >' – higi

+0

과 같은 문제가 있습니다. 이는이 다른 응용 프로그램이 XML을 이해할 수 없으며 수정되어야 함을 의미합니다. – Tomalak

답변

1

이이 도움이 될

private string content; 

    [XmlText] 
    public string Content 
    { 
     get { return content; } 
     set { content = XElement.Parse(value).Value; } 
    } 
1

먼저

public XmlCDataSection ProjectXml { get; set; } 
XmlCDataSection로 속성을 선언 이 경우

가 projectXml 당신이 당신의 메시지를 직렬화 할 때 당신은 당신의 좋은 형식 (통지)에 StackOverflow에 제시된 솔루션의 대부분은 직렬화 작동

<?xml version="1.0" encoding="utf-16"?> 
<MessageBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Message_ProjectStatusChanged"> 
    <ID>131</ID> 
    <HandlerName>Plugin</HandlerName> 
    <NumRetries>0</NumRetries> 
    <TriggerXml><![CDATA[<?xml version="1.0" encoding="utf-8"?><TmData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="9.0.0" Date="2012-01-31T15:46:02.6003105" Format="1" AppVersion="10.2.0" Culture="en-US" UserID="0" UserRole=""><PROJECT></PROJECT></TmData>]]></TriggerXml> 
    <MessageCreatedDate>2012-01-31T20:28:52.4843092Z</MessageCreatedDate> 
    <MessageStatus>0</MessageStatus> 
    <ProjectId>0</ProjectId> 
    <UserGUID>8CDF581E44F54E8BAD60A4FAA8418070</UserGUID> 
    <ProjectGUID>5E82456F42DC46DEBA07F114F647E969</ProjectGUID> 
    <PriorStatus>0</PriorStatus> 
    <NewStatus>3</NewStatus> 
    <ActionDate>0001-01-01T00:00:00</ActionDate> 
</MessageBase> 
0

있을 것이다 문자열 XML

ProjectXml = new XmlDocument().CreateCDataSection(projectXml); 

입니다, 비 직렬화가 아닙니다. 이 작업은 작업을 수행 할 것이고 코드에서 값을 가져 오거나 설정해야하는 경우 추가 한 추가 속성 PriceUrlByString을 사용하십시오.

private XmlNode _priceUrl; 
    [XmlElement("price_url")] 
    public XmlNode PriceUrl 
    { 
     get 
     { 
      return _priceUrl; 
     } 
     set 
     { 
      _priceUrl = value; 
     } 
    } 

    [XmlIgnore] 
    public string PriceUrlByString 
    { 
     get 
     { 
      // Retrieves the content of the encapsulated CDATA 
      return _priceUrl.Value; 
     } 

     set 
     { 
      // Encapsulate in a CDATA XmlNode 
      XmlDocument xmlDocument = new XmlDocument(); 
      this._priceUrl = xmlDocument.CreateCDataSection(value); 
     } 
    }