2013-09-28 1 views
0

я пытаюсь произвести XML, как показано нижеКак писать/установить суб-узел с множественным CDATA XML C#

<?xml version="1.0" encoding="UTF-8"?> 
<movie> 
<title><![CDATA[Con Air]]></title> 
<plot><![CDATA[When the government puts all its rotten criminal eggs in one airborne basket, it's asking for trouble. Before you can say, "Pass the barf bag," the crooks control the plane, led by creepy Cyrus "The Virus" Grissom. Watching his every move is the just-released Cameron Poe, who'd rather reunite with his family. The action climaxes with an incredible crash sequence in Las Vegas.]]></plot> 
<tagline><![CDATA[They were deadly on the ground; Now they have wings]]></tagline> 
<year>1997</year> 
<id>tt0118880</id> 
<rating>65</rating> 
<votes>93</votes> 
<budget>75000000</budget> 
<revenue>224012234</revenue> 
<company><![CDATA[Touchstone Pictures]]></company> 
<genre> 
<name><![CDATA[Action]]></name> 
<name><![CDATA[Adventure]]></name> 
<name><![CDATA[Thriller]]></name> 
</genre> 
</movie> 

Я использую C# XmlDocument для создания XML-файла с помощью сериализации класса ниже

[Serializable] 
public class movie 
{ 
    public movie() 
    { 
     this.genre = new Genre(); 
    } 

    [XmlIgnore] 
    public string title { get; set; } 
    [XmlElement("title")] 
    public XmlCDataSection titleCDATA 
    { 
     get { return new XmlDocument().CreateCDataSection(title); } 
     set { title = value.Value; } 
    } 

    [XmlIgnore] 
    public string plot { get; set; } 
    [XmlElement("plot")] 
    public XmlCDataSection plotCDATA 
    { 
     get { return new XmlDocument().CreateCDataSection(plot); } 
     set { plot = value.Value; } 
    } 
    [XmlIgnore] 
    public string tagline { get; set; } 
    [XmlElement("tagline")] 
    public XmlCDataSection taglineCDATA 
    { 
     get { return new XmlDocument().CreateCDataSection(tagline); } 
     set { tagline = value.Value; } 
    } 
    public int year { get; set; } 
    public string id { get; set; } 
    public int rating { get; set; } 
    public int votes { get; set; } 
    public long budget { get; set; } 
    public long revenue { get; set; } 

    [XmlIgnore] 
    public string company { get; set; } 
    [XmlElement("company")] 
    public XmlCDataSection companyCDATA 
    { 
     get { return new XmlDocument().CreateCDataSection(company); } 
     set { company = value.Value; } 
    } 

    [XmlElement("genre")] 
    public Genre genre { get; set; } 


} 

public class Genre 
{ 
    public Genre() 
    { 
    } 

    public string[] name { get; set; } 
} 

Но выход, как ожидалось, как показано ниже

<?xml version="1.0" encoding="utf-16"?> 
<movie> 
<title><![CDATA[Monster House]]></title> 
<plot><![CDATA[Monsters under the bed are scary enough, but what happens when an entire house is out to get you? Three teens aim to find out when they go up against a decrepit neighboring home and unlock its frightening secrets.]]></plot> 
<tagline><![CDATA[The House is . . . ALIVE!]]></tagline> 
<year>2006</year> 
<id>tt0385880</id> 
<rating>57</rating> 
<votes>74</votes> 
<budget>0</budget> 
<revenue>0</revenue> 
<company><![CDATA[Sony Pictures Entertainment]]></company> 
<genre> 
<name> 
<string>Adventure</string> 
<string>Animation</string> 
<string>Comedy</string> 
<string>Fantasy</string> 
<string>Mystery</string> 
<string>Science Fiction</string> 
<string>Family</string> 
</name> 
</genre> 
</movie> 

Как я могу создать суб-узел XML, как жанровый подузел в первом экс достаточно?

ответ

0

Я думаю, что ваша проблема с именами элементов, называемыми «строка», а не «имя», должна быть решена путем использования правильной аннотации, как описано в Controlling XML Serialization Using Attributes.

Вы должны изменить класс You немного (и я не уверен, что вы можете получить CDATA для имен, но это должно быть в порядке):

public class Movie 
{ 
    .... 
    [XmlArray("genre")] 
    [XmlArrayItem("Name")] 
    public string[] Genre { get; set; } 
} 
+0

Привет, спасибо за ответ, и я также измените тип string [] на XmlCDataSection [], поэтому вывод XmlArrayItem в CDATA, как и первый пример. Здесь вы найдете код:

public class Movie { .... [XmlArray("genre")] [XmlArrayItem("Name")] public XmlCDataSection[] Genre { get; set; } }
Surya

+0

Привет, спасибо за ответ, а также меняю тип строки [] на XmlCDataSection [], поэтому вывод XmlArrayItem в CDATA, как и в первом примере.
Вот код: ' общественного класса Movie { .... [XmlArray ("жанр")] [XmlArrayItem ("Name")] общественного XmlCDataSection [] Жанр {получить; задавать; } } ' – Surya