2

How would i write the xml out like

<?xml version="1.0" encoding="UTF-8"?>
<calibration>
  <ZoomLevel 250>0.0100502512562814</ZoomLevel 250>
  <ZoomLevel 250>0.0100502512562814</ZoomLevel 250>
  ........
</calibration>

I know how to write it out but i cant write it out in a loop which i need to atm the i have for writting the xml sheet is

public void XMLWrite(Dictionary<string, double> dict)
{
    //write the dictonary into an xml file
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);

    XmlNode productsNode = doc.CreateElement("calibration");
    doc.AppendChild(productsNode);

    foreach (KeyValuePair<string, double> entry in dict)
    {
        XmlNode zoomNode = doc.CreateElement("ZoomLevel");

        XmlAttribute ZoomLevel = doc.CreateAttribute(entry.Key.ToString());
        //XmlElement PixelSize = doc.CreateElement (entry.key = entry.Value.ToString());
        zoomNode.Attributes.Append(ZoomLevel);

        productsNode.AppendChild(zoomNode);
    }
    doc.Save(pathName);
}
1
  • 7
    <ZoomLevel 250> isn't a valid XML tag. What does your code output at the moment? Commented Apr 10, 2013 at 16:43

2 Answers 2

6

As the others said your wanted xml isn't valid. Another thing that I noticed is that in your example there are two nodes with the level zoom of 250 which is a key of the dictionary and as you know it should be unique. However I recommend you to use LINQ to XML (System.Xml.Linq) which is simpler, so what about:

public void XMLWrite( Dictionary<string, double> dict ) {
   //LINQ to XML
   XDocument doc = new XDocument( new XElement( "calibration" ) );

   foreach ( KeyValuePair<string, double> entry in dict )
     doc.Root.Add( new XElement( "zoom", entry.Value.ToString( ), new XAttribute( "level", entry.Key.ToString( ) ) ) );

   doc.Save( pathName );
}

I tested this code by passing this dictionary:

"250", 0.110050251256281
"150", 0.810050256425628
"850", 0.701005025125628
"550", 0.910050251256281

And the result is:

<?xml version="1.0" encoding="utf-8"?>
<calibration>
  <zoom level="250">0,110050251256281</zoom>
  <zoom level="150">0,810050256425628</zoom>
  <zoom level="850">0,701005025125628</zoom>
  <zoom level="550">0,910050251256281</zoom>
</calibration>
Sign up to request clarification or add additional context in comments.

Comments

0

As Michiel pointed out in the comments, the XML you want to create is not valid. As of the W3C XML specification:

Almost all characters are permitted in names, except those which either are or reasonably could be used as delimiters.

You might want to generate something like this instead:

<?xml version="1.0" encoding="UTF-8"?>
<calibration>
  <zoom level="250">0.0100502512562814</zoom>
  <zoom level="260">0.0100502512562815</zoom>
</calibration>

Generated with such a code snippet:

foreach (KeyValuePair<string, double> entry in dict)
{
  var node = doc.CreateElement("zoom");

  var attribute = doc.CreateAttribute("level");
  attribute.Value = entry.Key;

  node.InnerText = entry.Value.ToString(CultureInfo.InvariantCulture);

  node.Attributes.Append(attribute);

  productsNode.AppendChild(node);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.