예제 #1
0
        public static void SaveSudoku(WholeGrid grid, string filepath)
        {
            filepath = filepath.EndsWith(".xml") ? filepath : filepath + ".xml";
            XmlWriter xmlwriter = XmlWriter.Create(filepath, XML_Formatting.GetSettings());

            xmlwriter.WriteStartDocument();
            xmlwriter.WriteStartElement("sudoku");
            for (short X = 0; X < 3; X++)
            {
                for (short Y = 0; Y < 3; Y++)
                {
                    xmlwriter.WriteStartElement("Cube");
                    xmlwriter.WriteAttributeString("x", X.ToString());
                    xmlwriter.WriteAttributeString("y", Y.ToString());
                    for (short x = 0; x < 3; x++)
                    {
                        xmlwriter.WriteStartElement("subcolumn");
                        xmlwriter.WriteAttributeString("x", x.ToString());
                        for (short y = 0; y < 3; y++)
                        {
                            xmlwriter.WriteStartElement("val");
                            xmlwriter.WriteAttributeString("y", y.ToString());
                            xmlwriter.WriteValue(grid[X][Y][x][y]);
                            xmlwriter.WriteEndElement();
                        }
                        xmlwriter.WriteEndElement();
                    }
                    xmlwriter.WriteEndElement();
                }
            }
            xmlwriter.WriteEndElement();
            xmlwriter.WriteEndDocument();
            xmlwriter.Close();
        }
예제 #2
0
        public static bool ParseSudoku(string filepath, out WholeGrid grid)
        {
            filepath = filepath.EndsWith(".xml") ? filepath : filepath + ".xml";
            if (!File.Exists(filepath))
            {
                grid = null;
                return(false);
            }
            grid = new WholeGrid();
            XmlReader xmlreader = XmlReader.Create(filepath);

            while (xmlreader.Read())
            {
                if (xmlreader.IsStartElement("Cube"))
                {
                    int       X    = int.Parse(xmlreader["x"]);
                    int       Y    = int.Parse(xmlreader["y"]);
                    XmlReader cube = xmlreader.ReadSubtree();
                    while (cube.Read())
                    {
                        if (cube.IsStartElement("subcolumn"))
                        {
                            int       x         = int.Parse(cube["x"]);
                            XmlReader subcolumn = xmlreader.ReadSubtree();
                            while (subcolumn.Read())
                            {
                                if (subcolumn.IsStartElement("val"))
                                {
                                    int y = int.Parse(subcolumn["y"]);
                                    subcolumn.Read();
                                    grid[X][Y][x][y] = int.Parse(subcolumn.Value);
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }