Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Linq to Xml Mystery

$
0
0

Hi: Sorry about the title but I really do not know how to qualify this problem. I am writing code to create a xml element and it all works. Then I refactor the code and want to put the linq to xml in a static class method that returns an xelement. This also seems to work fine until it hits the xml parser and I get an error. I found the error but I don't know why it is being created.

The error is an "x:" is appearing in the xml returned from the static method. Here is the xml and please note the problem is only in the part that uses the static class method and not the rest of the xml.

<DrawingGroup><DrawingGroup.Children><GeometryDrawing Brush="#FFFFFFFF" Geometry="F1 M 50.035,24.9931L 0.0350494,24.9931"><GeometryDrawing.Pen><Pen Thickness="3" LineJoin="Round" Brush="#FF000000" /></GeometryDrawing.Pen></GeometryDrawing><x:GeometryDrawing Brush="#FF000000" Geometry="F1 M 0.578171091445398,19.3333333333333L 44.2448377581121,19"><x:GeometryDrawing.Pen><x:Pen Thickness="3" LineJoin="Round" Brush="#FF000000" /></x:GeometryDrawing.Pen></x:GeometryDrawing></DrawingGroup.Children></DrawingGroup>

Here is the static method:

//this will return the XElements for drawing lines
        public static XElement LineXml(String lineColor, String linePosition, String lineThickness)
        {
            XElement myXml = new XElement(ns + "GeometryDrawing",
                                                          new XAttribute("Brush", lineColor),
                                                          new XAttribute("Geometry", linePosition),
                                                           new XElement(ns + "GeometryDrawing.Pen",
                                                               new XElement(ns + "Pen",
                                                                   new XAttribute("Thickness", lineThickness),
                                                                   new XAttribute("LineJoin", "Round"),
                                                                   new XAttribute("Brush", lineColor))));

            return myXml;
        }

And here is the calling method. Note that it only calls the static method once and the rest of it uses the same xml and works fine.

//make string that is the color the user selected
                    String userColor = elementColor.Color.ToString();
                    //CALLS THE STATIC METHOD THAT GIVES THE ERROR
                    //THE ORIGINAL myInsertOff is commented out but works
                    XElement myInsertOff = ModifyXml.LineXml(userColor, linePosition, strokeThicknessString);
                    /*
                    XElement myInsertOff = new XElement(ns + "GeometryDrawing",
                                                           new XAttribute("Brush", userColor),
                                                           new XAttribute("Geometry", linePosition),
                                                            new XElement(ns + "GeometryDrawing.Pen",
                                                                new XElement(ns + "Pen",
                                                                    new XAttribute("Thickness", strokeThicknessString),
                                                                    new XAttribute("LineJoin", "Round"),
                                                                    new XAttribute("Brush", userColor))));
                     */

                    //add the off element to the list at item 0
                    myList.Add(myInsertOff);
                    // Make the on element
                    //color blue = FF0096FF
                    //XElement myInsertOn = ModifyXml.LineXml(userColor, linePosition, strokeThicknessString);
                    XElement myInsertOn = new XElement(ns + "GeometryDrawing",
                                                           new XAttribute("Brush", userColor),
                                                           new XAttribute("Geometry", linePosition),
                                                            new XElement(ns + "GeometryDrawing.Pen",
                                                                new XElement(ns + "Pen",
                                                                    new XAttribute("Thickness", strokeThicknessString),
                                                                    new XAttribute("LineJoin", "Round"),
                                                                    new XAttribute("Brush", userColor))));
                    //add the on element to the list at item 1
                    myList.Add(myInsertOn);
                    // Make the alarm element
                    //color red = FFFF0000
                    //XElement myInsertAlarm = ModifyXml.LineXml(userColor, linePosition, strokeThicknessString);
                    XElement myInsertAlarm = new XElement(ns + "GeometryDrawing",
                                                           new XAttribute("Brush", userColor),
                                                           new XAttribute("Geometry", linePosition),
                                                            new XElement(ns + "GeometryDrawing.Pen",
                                                                new XElement(ns + "Pen",
                                                                    new XAttribute("Thickness", strokeThicknessString),
                                                                    new XAttribute("LineJoin", "Round"),
                                                                    new XAttribute("Brush", userColor))));
                    //add the alarm element to the list at item 2
                    myList.Add(myInsertAlarm);

                }
                return myList;

 

Mike Gallinger C.Tech. Cutting Edge Computing Software Developer


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>