Possible issue with XamlReader
Juergen Boehm
1
Reputation point
I used the following code to parse a certain file with System.Windows.Markup.XamlReader
#region TryOutXamlParser
string xamlUserDir = "\\Users\\Boehm.J";
string xamlPathDir = "Documents\\dxf\\DXF";
string xamlFileName = "test-error-961.xaml";
string xamlFile = xamlUserDir + "\\" + xamlPathDir + "\\" + xamlFileName;
if (File.Exists(xamlFile))
{
FileStream xamlStream = File.OpenRead(xamlFile);
Canvas pathCanvas = (Canvas)XamlReader.Load(xamlStream);
UIElementCollection paths = ((Canvas)pathCanvas.Children[0]).Children;
//here the parse error is already apparent
//in string control several "C" opcodes have been lost
string control = XamlWriter.Save(pathCanvas);
int len = paths.Count;
Trace.WriteLine($"paths.Count = {len}");
int maxPcCount = 0;
foreach (System.Windows.Shapes.Path pp in paths)
{
string name = pp.Name;
Geometry currGeom = pp.Data as Geometry;
string geomStr = pp.Data.ToString(); //XamlWriter.Save(currGeom);
string geomStr1 = "<PathGeometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
"Figures=\"" + geomStr + "\" />";
PathGeometry pg = (PathGeometry)XamlReader.Parse(geomStr1);
PathFigureCollection pfc = pg.Figures;
foreach (PathFigure pfAkt in pfc)
{
foreach (PathSegment pseg in pfAkt.Segments)
{
string psegStr = pseg.ToString();
//Trace.WriteLine($"{psegStr}");
switch (pseg)
{
case LineSegment ls:
break;
case ArcSegment arcs :
break;
case BezierSegment bseg:
maxPcCount = Math.Max(3, maxPcCount);
break;
case QuadraticBezierSegment qbs:
maxPcCount = Math.Max(2, maxPcCount);
break;
case PolyBezierSegment pbs:
PointCollection pc = pbs.Points;
maxPcCount = Math.Max(pc.Count, maxPcCount);
break;
default:
Debug.Assert(false);
break;
}
}
}
}
Trace.WriteLine($"maxPcCount = {maxPcCount}");
}
else
{
Trace.WriteLine($"LoadTestDataViewModel.Load: Could not find {xamlFile}");
}
#endregion
It parses the following file
(extension renamed from .xaml to .txt because of upload-filter).
erroneously. In Path.Data several "C" (cubic spline) command codes are lost and the string is passed as having a higher order bezier curve instead of several cubic splines as specified in the original file.
Sign in to answer