简体   繁体   English

在循环C#中编写XML

[英]Writing XML in loop c#

How would i write the xml out like 我将如何将xml写出来

<?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 我知道如何写出来,但我不能在循环中写出来,我需要在atm上写xml表

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);
}

As the others said your wanted xml isn't valid. 正如其他人所说,您想要的xml无效。 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 . 我注意到的另一件事是,在您的示例中,有两个节点的级别缩放为250 ,这是字典的键,并且您知道它应该是唯一的 However I recommend you to use LINQ to XML ( System.Xml.Linq ) which is simpler, so what about: 但是,我建议您使用更简单的LINQ to XMLSystem.Xml.Linq ),那么:

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>

As Michiel pointed out in the comments, the XML you want to create is not valid. 正如Michiel在评论中指出的那样,您要创建的XML无效。 As of the W3C XML specification : W3C XML规范开始

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM