简体   繁体   English

使用LINQ编写XML

[英]Writing XML using LINQ

I'm using a piece of code for writing a XML file (using LINQ)(was using XMLWriter and code was getting too dirty, LINQ is cleaner and seems to be faster). 我正在使用一段代码来编写XML文件(使用LINQ)(使用XMLWriter,代码变得太脏了,LINQ更干净,而且看起来更快)。

The question is: It writes the XML but only the first part of it - i have other statements inside an if who aren't being written. 问题是:它只写XML,但只写XML的第一部分-如果没有写谁,我里面还有其他语句。

I set some breakpoints where the if happens and found that everything is happening there as it should be (variables are getting their values, etc) - only the XML isn't writing. 我在if发生的地方设置了一些断点,发现一切都按原样进行(变量正在获取其值,等等)-只有XML不在编写。

On the first part of the code you may notice many closing parentheses - without 'em i can't get the XML to work - or, if i try to change their order (like closing sooner) when trying to convert it says XML will be on a invalid format. 在代码的第一部分,您可能会注意到许多结束括号-如果没有它们,我将无法使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

Your code seems missing parts. 您的代码似乎缺少部分。 As it is, I don't think it could even compile. 就这样,我认为它甚至无法编译。 Indentation is incoherent and some of the variables aren't even defined, so it's hard to see where your problem actually lies. 缩进是不连贯的,甚至没有定义某些变量,因此很难看到问题的根源。

Nevertheless, I think I may know a trick to help you get your elements in order. 不过,我想我可能知道一个技巧,可以帮助您使元素井井有条。 You may use a separate private method to yield all the elements you want according to a given list of colors. 您可以使用单独的私有方法根​​据给定的颜色列表产生所需的所有元素。

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