0

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

我在这里有我的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>

问题是为什么会发生这种错误?*** 我注意到一件奇怪的事情,如果我保存文件就可以了,但是如果我删除它附加到我的文本的文本,我删除的所有内容。

4

1 回答 1

0

很奇怪……这很可能是由于序列化过程中 ContentText 中的某些字符导致括号不匹配的结果。如果将 XmlWriterSettings 编码属性设置为 unicode,会发生什么情况?

于 2013-02-06T15:49:43.287 回答