public void Xml_Tests() { CreateFigures factory = new CreateFigures(); Box figures = new Box(); float[] vs = new float[] { 3f, 2f }; float[] vs1 = new float[] { 31f }; float[] vs2 = new float[] { 3f, 2.1f, 5f }; float[] vs3 = new float[] { 11f }; float[] vs4 = new float[] { 17f, 0.9f }; float[] vs5 = new float[] { 9f, 7f }; figures.AddFigure(factory.CreateFigure(Material.Film, Form.Rectangle, vs)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Circle, vs1)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Triangle, vs2)); figures.AddFigure(factory.CreateFigure(Material.Film, Form.Square, vs3)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs4)); figures.AddFigure(factory.CreateFigure(Material.Paper, Form.Rectangle, vs5)); figures.SaveXMLAll("output_1_all.xml"); figures.SaveXMLFilm("output_1_film.xml"); figures.SaveXMLPaper("output_1_paper.xml"); figures.SaveXML2All("output_2_all.xml"); figures.SaveXML2Film("output_2_film.xml"); figures.SaveXML2Paper("output_2_paper.xml"); figures.LoadXML("input1.xml"); figures.LoadXML2("input2.xml"); figures.SaveXMLAll("output_i1_all.xml"); figures.SaveXML2All("output_i2_all.xml"); Assert.IsNotNull(figures); }
/// <summary> /// Method Read /// </summary> /// <returns>New List</returns> public List <IFigure> Read(string input) { List <IFigure> boxxml = new List <IFigure>(); XmlReader xreader = XmlReader.Create(input); Material material; Colors color = 0; CreateFigures factory = new CreateFigures(); while (xreader.Read()) { if (xreader.Name == "figure") { switch (xreader.GetAttribute("type")) { case "Circle": { float d; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "diameter") { d = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Circle, new float[] { d }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Square": { float a; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "height") { a = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { a }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Rectangle": { float h, w; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "height") { h = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "width") { w = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Rectangle, new float[] { h, w }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Triangle": { float a, b, c; xreader.Read(); xreader.Read(); if (xreader.Name == "material") { material = (Material)Enum.Parse(typeof(Material), xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "color") { if (material == Material.Paper) { color = (Colors)Enum.Parse(typeof(Colors), xreader.ReadInnerXml()); xreader.Read(); } else { xreader.ReadInnerXml(); xreader.Read(); } } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_a") { a = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_b") { b = float.Parse(xreader.ReadInnerXml()); xreader.Read(); } else { throw new Exception("Wrong input xml"); } if (xreader.Name == "side_c") { c = float.Parse(xreader.ReadInnerXml()); } else { throw new Exception("Wrong input xml"); } IFigure figure = factory.CreateFigure(material, Form.Triangle, new float[] { a, b, c }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } } } } xreader.Close(); return(boxxml); }
/// <summary> /// StreamReader /// </summary> /// <returns>List of figures from XML</returns> public List <IFigure> Read(string input) { CreateFigures factory = new CreateFigures(); List <IFigure> boxxml = new List <IFigure>(); StreamReader sr = new StreamReader(input); string doc = sr.ReadToEnd(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(doc); XmlElement xRoot = xdoc.DocumentElement; foreach (XmlNode xnode in xRoot) { XmlNode typefigure = xnode.Attributes.GetNamedItem("type"); switch (typefigure.Value) { case "Circle": { Material material = 0; Colors color = 0; float diameter = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "diameter") { diameter = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Circle, diameter); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Rectangle": { Material material = 0; Colors color = 0; float height = 1; float width = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "height") { height = float.Parse(childnode.InnerText); } if (childnode.Name == "width") { width = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Rectangle, new float[] { height, width }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Square": { Material material = 0; Colors color = 0; float height = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "height") { height = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { height }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } case "Triangle": { Material material = 0; Colors color = 0; float a = 1; float b = 1; float c = 1; foreach (XmlNode childnode in xnode.ChildNodes) { if (childnode.Name == "material") { material = (Material)Enum.Parse(typeof(Material), $"{childnode.InnerText}"); } if (material == Material.Paper) { if (childnode.Name == "color") { color = (Colors)Enum.Parse(typeof(Colors), $"{childnode.InnerText}"); } } if (childnode.Name == "side_a") { a = float.Parse(childnode.InnerText); } if (childnode.Name == "side_b") { b = float.Parse(childnode.InnerText); } if (childnode.Name == "side_c") { c = float.Parse(childnode.InnerText); } } IFigure figure = factory.CreateFigure(material, Form.Square, new float[] { a, b, c }); if (material == Material.Paper && color != Colors.white) { ((IPaper)figure).Color = color; } boxxml.Add(figure); break; } } } sr.Close(); return(boxxml); }