使用LINQ编写XML

时间:2016-02-15 22:38:59

标签: c# .net xml linq

我使用一段代码编写XML文件(使用LINQ)(使用XMLWriter并且代码变得太脏了,LINQ更干净,似乎更快)。

问题是:它写的只是XML的第一部分 - 如果没有写的话,我还有其他的陈述。

我设置了一些断点,其中if发生了​​,发现一切都在那里发生(变量正在获取它们的值等) - 只有XML不能写。

在代码的第一部分,您可能会注意到许多结束括号 - 没有' em我无法使XML工作 - 或者,如果我尝试更改其顺序(如更快关闭)时试图转换它说XML将是无效的格式。

    private void openXMLToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string currentcolor;
        XElement xmldoc = new XElement("JMF",
            new XAttribute("SenderID", "InkZone-Controller"),
            new XAttribute("version", "1.2"),
        //new XAttribute("xmlns", "http://www.CIP4.org/JDFSchema_1_1"),

        new XElement("Command",
        new XAttribute("ID", "cmd.00695"),
        new XAttribute("Type", "Resource"),
        new XElement("ResourceCmdParams",
        new XAttribute("Resourcename", "InkZoneProfile"),
        new XAttribute("JobID", "K_41")),

        new XElement("InkZoneProfile",
        new XAttribute("ID", "r0013"),
        new XAttribute("Class", "Parameter"),
        new XAttribute("Locked", "False"),
        new XAttribute("Status", "Available"),
        new XAttribute("PartIDKeys", "SignatureName SheetName Side Separation"),
        new XAttribute("DescriptiveName", "Schieberwerte von DI"),
        new XAttribute("ZoneWidth", "32")),

        new XElement("InkZoneProfile",
        new XAttribute("SignatureName", "SIG1")),

        new XElement("InkzoneProfile",
        new XAttribute("Locked", "False"),
        new XAttribute("Sheetname", "S1")),

        new XElement("InkZoneProfile",
        new XAttribute("Side", "Front")),

                //Loop for getting black values and store them on XML
                    for(int i=0; i<stringsize; i++)
                        {
                            currentcolor = colors[i];
                            if(currentcolor == "Black")
                                {
                                    //Extracting numbers from blackzones
                                    Regex regex1 = new Regex(@"HDMInkB \[(.*?)\]",
                                        RegexOptions.Singleline | RegexOptions.Multiline);
                                    var v1 = regex1.Match(hdmzones);
                                    string blackzones = v1.Groups[1].ToString();
                                    //Converting to string - add a delimiter into each space
                                    blackzones = Regex.Replace(blackzones, @"\s+", "|");
                                    Double[] numbers; //An array of Doubles - store numbers separated
                                    string[] numbers_str = blackzones.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                                    numbers = new Double[numbers_str.Length];
                                    //Loop trough numbers
                                        for (int j = 0; j < numbers.Length; j++)
                                        {
                                            numbers[j] = Double.Parse(numbers_str[j], CultureInfo.InvariantCulture);
                                            //Converts each number on the string to a Double number, store it in a position
                                            //in the Double array
                                            numbers[j] = numbers[j] / 100; //Needed calculus
                                            numbers[j] = Math.Round(numbers[j], 3); //Storing numbers rounded
                                        }
                                    string blackvalues = String.Join(" ", numbers.Select(f => f.ToString()));
                //Converting values back to string - so i can insert on the XML without problems
                new XElement("InkZoneProfile",
                    new XAttribute("Separation", currentcolor),
                    new XAttribute("ZoneSettingsX", blackvalues));


            }//Closing BLACK if Statement

                        }//Closing for statement for XMLAttribute
                                //Saving XML Document
                                string strPath = Environment.GetFolderPath(
                                 System.Environment.SpecialFolder.DesktopDirectory);

                                string path2 = "new_xml.xml";
                                string combined = Path.Combine(strPath, path2);
                                xmldoc.Save(combined);


    }//Closing ConvertXML

1 个答案:

答案 0 :(得分:0)

您的代码似乎缺少部分。事实上,我认为它甚至无法编译。缩进是不连贯的,有些变量甚至没有定义,所以很难看出你的问题究竟在哪里。

尽管如此,我想我可能知道一个技巧来帮助你按顺序排列元素。您可以使用单独的私有方法根​​据给定的颜色列表生成所需的所有元素。

private IEnumerable<XElement> GetBlackvaluesElements(string hdmzones, params string[] colors)
{
    for (int i = 0; i < colors.Length; i++)
    {
        currentColor = color[i];
        if (currentColor = "Black")
        {
            /* Regex, Calculus & other stuff to get string blackvalues here */
            yield return new XElement("InkZoneProfile",
                new XAttribute("Separation", currentColor),
                new XAttribute("ZoneSettingsX", blackvalues));
        }
    }
}

private void OpenXMLToolStripMenuItem_Click(object sender, EventArgs e)
{
    var commandElement = new XElement("Command");
    /* Add hardcoded elements to commandElement here */
    // Adding blackvalues to commandElement
    commandElement.Add(GetBlackvaluesElements(hdmzones, colors).ToArray());

    var xmldoc = new XElement("JMF",
        /* add required attributes here */
        commandElement);

    /* Save document here */
}
相关问题