16.04.2015, 06:35
#38937896
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
Участник
Сообщения: 6 318
Рейтинг:
0
/ 0
|
|
|
|
Ну CDATA же:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78.
namespace wpftest2
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
SaveXamlCommand = new SimpleCommand(SaveXaml);
DataContext = this;
}
public ICommand SaveXamlCommand { get; private set; }
void SaveXaml()
{
var defaultStyleKey = GetValue(DefaultStyleKeyProperty);
var style = Application.Current.FindResource(defaultStyleKey) as Style;
var sb = new StringBuilder();
var xws = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineOnAttributes = true };
using (var sr = new StringWriter(sb))
using (var xw = XmlWriter.Create(sr, xws))
XamlWriter.Save(style, xw);
var st = new SerializationTest {XamlText = sb.ToString()};
var serializer = new XmlSerializer(typeof (SerializationTest));
using(var fs = new FileStream(@"D:\Trash\456.xml", FileMode.Create))
using(var xw = XmlWriter.Create(fs, xws))
serializer.Serialize(xw, st);
SerializationTest st1;
using (var fs = new FileStream(@"D:\Trash\456.xml", FileMode.Open))
using (var xw = XmlReader.Create(fs))
st1 = (SerializationTest)serializer.Deserialize(xw);
var xd = new XmlDocument();
xd.LoadXml(st1.XamlText);
sb.Clear();
using (var sr = new StringWriter(sb))
using (var xw = XmlWriter.Create(sr, xws))
xd.Save(xw);
Console.WriteLine(sb.ToString());
}
}
[XmlRoot]
public class SerializationTest
{
[XmlIgnore]
public string XamlText { get; set; }
[XmlElement]
public XmlCDataSection CDataContent
{
get { return new XmlDocument().CreateCDataSection(XamlText); }
set { XamlText = value.Value; }
}
}
public class SimpleCommand : ICommand
{
readonly Action _action;
public SimpleCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return _action!=null;
}
public event EventHandler CanExecuteChanged;
}
}
XML результата сериализации: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100.
<?xml version="1.0" encoding="utf-8"?>
<SerializationTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CDataContent><![CDATA[<?xml version="1.0" encoding="utf-16"?>
<Style
TargetType="Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style.Triggers>
<Trigger
Property="Window.ResizeMode">
<Setter
Property="Control.Template">
<Setter.Value>
<ControlTemplate
TargetType="Window">
<Border
BorderThickness="{TemplateBinding Border.BorderThickness}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}">
<Grid>
<AdornerDecorator>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
</AdornerDecorator>
<ResizeGrip
IsTabStop="False"
Name="WindowResizeGrip"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Visibility="Collapsed" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition
Property="Window.ResizeMode"
Value="{x:Static ResizeMode.CanResizeWithGrip}" />
<Condition
Property="Window.WindowState"
Value="{x:Static WindowState.Normal}" />
</MultiTrigger.Conditions>
<Setter
Property="UIElement.Visibility"
TargetName="WindowResizeGrip">
<Setter.Value>
<x:Static
Member="Visibility.Visible" />
</Setter.Value>
</Setter>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Trigger.Value>
<x:Static
Member="ResizeMode.CanResizeWithGrip" />
</Trigger.Value>
</Trigger>
</Style.Triggers>
<Style.Resources>
<ResourceDictionary />
</Style.Resources>
<Setter
Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource
ResourceKey="{x:Static SystemColors.WindowTextBrushKey}" />
</Setter.Value>
</Setter>
<Setter
Property="Panel.Background">
<Setter.Value>
<DynamicResource
ResourceKey="{x:Static SystemColors.WindowBrushKey}" />
</Setter.Value>
</Setter>
<Setter
Property="Control.Template">
<Setter.Value>
<ControlTemplate
TargetType="Window">
<Border
BorderThickness="{TemplateBinding Border.BorderThickness}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}">
<AdornerDecorator>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
</AdornerDecorator>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>]]></CDataContent>
</SerializationTest>
|
|