将页面内容序列化为xml文件

时间:2013-02-06 15:36:04

标签: c# xml-serialization

我有一个非常奇怪的错误,继续......

我的xml在这里:


这是正确的!

<?xml version="1.0" encoding="utf-8"?>
<Page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Title Text</Title>
  <Subtitles>
    <Subtitle index="0">
      <Text>Sub title</Text>
    </Subtitle>
    <Subtitle index="1">
      <Text>Sub title</Text>
    </Subtitle>
  </Subtitles>
  <Content>
    <Images>
      <ImageSource src="http://www.placehold.it/760x236" border-width="8px" border-color="#F4F2F3" />
    </Images>
    <Text>
      <![CDATA[here i need a place for a huge text's and stufff]]>
    </Text>
  </Content>
</Page>

直到这里,所有事情都没问题,在我将其序列化之后......

try
{
    XmlSerializer serializer = new XmlSerializer(typeof(TOut));

    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true
    };

    // get xml source file
    var source = string.Format("{0}\\{1}", SettingResolver<Settings>.BaseFolder, Resolver.Settings.Pages.PageCollection[pageName].Source);
    using (FileStream fs = new FileStream(source, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        XmlWriter writer = XmlWriter.Create(fs, settings);
        serializer.Serialize(writer, o);
    }
}
catch (Exception e)
{
    ExceptionHandler(new ErrorArgs("Serializing faulted!", e));
}

这是我遇到问题的元素......

public class Content
{
    [XmlElement("Images")]
    public Images Image { get; set; }

    [XmlIgnore]
    public String ContentText
    {
        get;
        set;
    }

    private static readonly XmlDocument DummyDoc = new XmlDocument();

    [XmlElement("Text")]
    public XmlCDataSection ContentTextCData
    {
        get { return DummyDoc.CreateCDataSection(ContentText); }
        set { ContentText = (value != null) ? value.Data : null; }
    }
}

我得到了这个XML的结果:
这个不合适,而

之后
<?xml version="1.0" encoding="utf-8"?>
<Page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Title Text</Title>
  <Subtitles>
    <Subtitle index="0">
        <Text>Sub title</Text>
    </Subtitle>
    <Subtitle index="1">
        <Text>Sub title</Text>
    </Subtitle>
  </Subtitles>
  <Content>
    <Images>
      <ImageSource src="http://www.placehold.it/760x236" border-width="8px" border-color="#F4F2F3" />
    </Images>
    <Text>  
    <![CDATA[here i need a place for a huge text's and stufff]]>         
    </Text>
  </Content>
</Page>------------------>
</Page>
here i need a place for a huge text's and stufff
here i need a place for a huge text's and stufff
here i need a place for a huge text's and stufff
 </Content>
</Page>

或者这个:

<?xml version="1.0" encoding="utf-8"?>
<Page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>title</Title>
  <Subtitles>
    <Subtitle index="0">
      <Text>test</Text>
    </Subtitle>
    <Subtitle index="1">
      <Text>test</Text>
    </Subtitle>
  </Subtitles>
  <Content>
    <Images>
      <ImageSource src="http://www.placehold.it/760x236" border-width="8px" border-color="#F4F2F3" />
    </Images>
    <Text><![CDATA[just a text + +++++++++++++++++++++++++Hropj
+++++++++++++++++++++++++Hropj
]]></Text>
  </Content>
</Page></Text>
  </Content>
</Page>

问题是为什么会发生这种错误? ***我注意到一个奇怪的事情,如果我保存文件没关系,但如果我删除文本,它会附加到我的文本,我删除的每一件事。

1 个答案:

答案 0 :(得分:0)

非常奇怪......这很可能是序列化期间ContentText中某些字符导致括号不匹配的结果。如果将XmlWriterSettings编码属性设置为unicode会发生什么?