Possible issue with XamlReader

Juergen Boehm 1 Reputation point
2021-07-27T15:53:29.447+00:00

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

118324-test-error-961.txt

(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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.