0

I have a very weird bug, going on....

I have my xml here:


This is correct!

<?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>

Up until here every thing is ok, after i serialize it like so...

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));
}

Here is the element that i have a problem with it...

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; }
    }
}

I get a result of this XML:
This one is not ok and the one after

<?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>

Or this:

<?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>

The question is why this kind of bug happens? *** I have noticed one weird thing, if i save the file it's ok, but if i delete the the text it appends to my text, every thing i deleted.

4

1 に答える 1

0

非常に奇妙です...これは、シリアル化中に ContentText の特定の文字によって引き起こされたブラケットの不一致の結果である可能性があります。XmlWriterSettings エンコーディング プロパティを unicode に設定するとどうなりますか?

于 2013-02-06T15:49:43.287 に答える