簡體   English   中英

使用LINQ編寫XML

[英]Writing XML using LINQ

我正在使用一段代碼來編寫XML文件(使用LINQ)(使用XMLWriter,代碼變得太臟了,LINQ更干凈,而且看起來更快)。

問題是:它只寫XML,但只寫XML的第一部分-如果沒有寫誰,我里面還有其他語句。

我在if發生的地方設置了一些斷點,發現一切都按原樣進行(變量正在獲取其值,等等)-只有XML不在編寫。

在代碼的第一部分,您可能會注意到許多結束括號-如果沒有它們,我將無法使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

您的代碼似乎缺少部分。 就這樣,我認為它甚至無法編譯。 縮進是不連貫的,甚至沒有定義某些變量,因此很難看到問題的根源。

不過,我想我可能知道一個技巧,可以幫助您使元素井井有條。 您可以使用單獨的私有方法根​​據給定的顏色列表產生所需的所有元素。

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 */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM