1

I've been trying to understand that for several hours but got no luck. I've a list based on a class:

public class xmldata //Class to receive items list
{
    public string xml_filename { get; set; }
    public string colorname { get; set; }
    public string colorvalues { get; set; }

}

Then i made a separated class to hold the list:

public class xmldatalist
{
    public List<xmldata> FullList = new List<xmldata>();
}

Each position of this list (FullList) contains three elements from xmldata - the specific one i want to focus is xml_filename.

My question is: How could i have a foreach loop to iterate over this list , and generate one XML file for each xml_filename value.

i.e: From positions 0 to 5 , xml_filename is file1.bla From positions 6 to 9 , xml_filename is file2.bla

I want a foreach that will iterate trough positions 0 to 5 , check that xml_filename is file1.bla and write attributes who are on position 0 to 5.

Something like:

Check xml_filename Create the file Write Attributes if xml_filename is the same on the next position keep writing on this file if xml_file is different on the next position, write new file

Right now i was trying to use Linq, by doing this:

foreach (var ListXmlFileName in XMLList.FullList.Where(x => x.xml_filename == XmlFilename))
{
}

But i'm kinda lost on how to proceed. Another approach i tried was:

foreach (xmldata XmlStuff in XmlItems.ItemsList)
{
}

Without success also. The code for writing the XML is:

public void writexml(xmldatalist XmlItems, variables GlobalVars)
{
    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "\t",
        NewLineChars = Environment.NewLine,
        NewLineHandling = NewLineHandling.Replace,
        Encoding = new UTF8Encoding(false)
    };
    int pos = 0;



    foreach (xmldata XmlStuff in XmlItems.ItemsList)
    {

        string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        string XmlFilename = "dadada";
        string FileExtension = ".xml";
        string PathString = Path.Combine(DesktopFolder, "XML");
        System.IO.Directory.CreateDirectory(PathString);
        string FullPath = Path.Combine(PathString, XmlFilename + FileExtension);

        XmlWriter XmlWriting = XmlWriter.Create(FullPath, settings);

            XmlWriting.WriteStartDocument();
            XmlWriting.WriteStartElement("JMF");
            XmlWriting.WriteAttributeString("SenderID", "InkZone-Controller");
            XmlWriting.WriteAttributeString("Version", "1.2");
            //XmlWriting.WriteAttributeString("xmlns","",null, "http://www.CIP4.org/JDFSchema_1_1");

            XmlWriting.WriteStartElement("Command");
            XmlWriting.WriteAttributeString("ID", "cmd.00695");
            XmlWriting.WriteAttributeString("Type", "Resource");


            XmlWriting.WriteStartElement("ResourceCMDParams");
            XmlWriting.WriteAttributeString("ResourceName", "InkZoneProfile");
            XmlWriting.WriteAttributeString("JobID", "K_41");


            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("ID", "r0013");
            XmlWriting.WriteAttributeString("Class", "Parameter");
            XmlWriting.WriteAttributeString("Locked", "false");
            XmlWriting.WriteAttributeString("Status", "Available");
            XmlWriting.WriteAttributeString("PartIDKeys", "SignatureName SheetName Side Separation");
            XmlWriting.WriteAttributeString("DescriptiveName", "Schieberwerte von DI");
            XmlWriting.WriteAttributeString("ZoneWidth", "32");


            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("SignatureName", "SIG1");

            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Locked", "False");
            XmlWriting.WriteAttributeString("SheetName", "S1");

            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Side", "Front");

I've tried this too but didn't worked as expected(The while loop)

       while(XmlStuff.xml_filename == XmlItems.ItemsList[pos].xml_filename)
        {
            XmlWriting.WriteStartElement("InkZoneProfile");
            XmlWriting.WriteAttributeString("Separation", XmlItems.ItemsList[pos].colorname);
            XmlWriting.WriteAttributeString("ZoneSettingsX", XmlItems.ItemsList[pos].colorvalues);
            XmlWriting.WriteEndElement();
            pos++;
        }
    XmlWriting.WriteEndDocument();
    XmlWriting.Close();     
    }
}

Any help will be greately appreciated. Thanks a lot.

**

EDIT: XML Example

**.

<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2" xmlns="http://www.CIP4.org/JDFSchema_1_1">
    <Command ID="cmd.00695" Type="Resource">
        <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
            <InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
                <InkZoneProfile SignatureName="SIG1">
                    <InkZoneProfile Locked="false" SheetName="S1">
                        <InkZoneProfile Side="Front">
                            <InkZoneProfile Separation="designer P&G 1901" ZoneSettingsX="0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088"/>
                            <InkZoneProfile Separation="designer P&G 0323" ZoneSettingsX="0.011 0.028 0.02 0.021 0.079 0.071 0.046 0.059 0.043 0.062 0.044 0.094 0.024 0.037 0.012 0.023"/>
                            <InkZoneProfile Separation="designer P&G 0068" ZoneSettingsX="0 0.04 0.03 0.016 0.08 0.005 0.041 0.036 0.034 0.044 0.028 0.072 0.001 0.005 0.063 0.008"/>
                            <InkZoneProfile Separation="designer P&G 0056" ZoneSettingsX="0 0.064 0.053 0.032 0.137 0.022 0.164 0.097 0.153 0.11 0.052 0.127 0.01 0.007 0.108 0.008"/>
                            <InkZoneProfile Separation="designer P&G 0306" ZoneSettingsX="0 0 0 0.19 0.187 0.259 0.224 0.178 0.24 0.185 0.249 0.188 0.173 0 0 0.004"/>
                            <InkZoneProfile Separation="designer P&G 0016" ZoneSettingsX="0.069 0.1 0.21 0.049 0.07 0.027 0.138 0.265 0.134 0.242 0.056 0.066 0.039 0.09 0.17 0.13"/>
                            <InkZoneProfile Separation="designer P&G 0057" ZoneSettingsX="0 0 0 0 0.023 0.001 0.012 0.012 0.008 0.016 0.003 0.021 0.001 0 0 0.004"/>
                            <InkZoneProfile Separation="Keyline" ZoneSettingsX="0.013 0.006 0.006 0.038 0.007 0.033 0.012 0.026 0.018 0.02 0.016 0.007 0.034 0.006 0.006 0.022"/>
                        </InkZoneProfile>
                    </InkZoneProfile>
                </InkZoneProfile>
            </InkZoneProfile>

This would be more accurate:

<InkZoneProfile Separation="Cyan" ZoneSettingsX="0.391 0.36 0.097 0.058 0 0 0 0 0 0 0 0 0.178 0.394 0.201 0.088"/>

Lets suppose this belongs to file1. File1 will have five SomeColor and SomeValues are in each position of the list.

Pablo Costa
  • 125
  • 4
  • 14
  • Does your list some things like `name_a, name_a, name_a, name_b, name_b, name_b, name_b, name_b,...` or it being random, like: `name_b, name_b, name_a, name_a, name_b,...`? – NoName Feb 25 '16 at 02:55
  • I didn't understood. Lemme try to rephrase. From positions 0 to 5 , every position have three items: colorname, colorvalues and xml_filename. colorname and colorvalues are going to be written to a xml file. xml_filename is the name of this file and is going to have the same value across positions 0 to 5. I need to do something like "group" those positions , write everything i need about them (to a xml file with the name of xml_filename) and move to the next. – Pablo Costa Feb 25 '16 at 03:04
  • @Sakura sorry now i understood your question. Yeah it follows this pattern. From positions 0 to 5 xml_filename is name_a. From positions 6 to 10 xml_filename is name_b , and so goes on... – Pablo Costa Feb 25 '16 at 03:09
  • I don't see your `xmldata` class in your XML file. Is this xml belong to other project? If so, ask other question and I will answer you. Each question you should only ask about one problem. I expect to see some thing like: ` D:\File_One Blue #0000FF ` in your xml content. – NoName Feb 25 '16 at 03:57
  • xml_filename is only for identification purposes - he won't go into the final xml. The class xmldata is on the top of the question (first text) , along with other classes. – Pablo Costa Feb 25 '16 at 04:06
  • What i need now is something like an "inner foreach" where it would iterate trough colorname. – Pablo Costa Feb 25 '16 at 04:20
  • Pablo Costa: See my edit. – NoName Feb 25 '16 at 04:23
  • @Sakura Thanks a lot ! It worked. I didn't knew about Serialize - guess i'll have to study about it. I see the problem you mentioned: Everything's being written onto one file only (data about the files loaded) , instead of going out and iterating \ generating another file. I'll post as a separate question. Thanks ! – Pablo Costa Feb 25 '16 at 04:45

1 Answers1

1

Is this what you want:

[Serializable]
[XmlType("InkZoneProfile")]
public class xmldata //Class to receive items list
{
    [XmlIgnore]
    public string xml_filename { get; set; }

    [XmlAttribute("Separation")]
    public string colorname { get; set; }

    [XmlAttribute("ZoneSettingsX")]
    public string colorvalues { get; set; }

}

FullList.Add(new xmldata { xml_filename = @"D:\File_One", colorname = "Red", colorvalues = "#FF0000" });
FullList.Add(new xmldata { xml_filename = @"D:\File_One", colorname = "Blue", colorvalues = "#0000FF" });
FullList.Add(new xmldata { xml_filename = @"D:\File_Two", colorname = "Black", colorvalues = "#000000" });
FullList.Add(new xmldata { xml_filename = @"D:\File_Two", colorname = "White", colorvalues = "#FFFFFF" });

Dictionary<string, List<xmldata>> xmlFiles = new Dictionary<string, List<xmldata>>();
foreach (var item in FullList)
{
    if (!xmlFiles.ContainsKey(item.xml_filename)) xmlFiles[item.xml_filename] = new List<xmldata>();
    xmlFiles[item.xml_filename].Add(item);
}

foreach (var i in xmlFiles)
{
    string yourXMLFileName = i.Key;
    List<xmldata> xmlDataOfThisFile = i.Value;
    Console.WriteLine(yourXMLFileName);
    try
    {
        using (FileStream fs = new FileStream(yourXMLFileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<xmldata>));
            serializer.Serialize(fs, FullList);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.InnerException);
    }
}

This will create this XML:

<?xml version="1.0"?>
<ArrayOfInkZoneProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <InkZoneProfile Separation="Red" ZoneSettingsX="#FF0000" />
  <InkZoneProfile Separation="Blue" ZoneSettingsX="#0000FF" />
  <InkZoneProfile Separation="Black" ZoneSettingsX="#000000" />
  <InkZoneProfile Separation="White" ZoneSettingsX="#FFFFFF" />
</ArrayOfInkZoneProfile>

EDIT

Based on your comment, you have an other problem. When the problem on the title question is solved now, you should ask new problem with other problem. Don't combine it here.

NoName
  • 7,940
  • 13
  • 56
  • 108
  • You populate it right before you write your data to disk. It can be OK inside `writexml` – NoName Feb 25 '16 at 03:36
  • Basically this. But how am i going to write the three fields from each list position - i didn't got this part. In a previous code , i had this: xmlWriter.WriteStartElement("InkZoneProfile"); xmlWriter.WriteAttributeString("Separation", currentcolor); xmlWriter.WriteAttributeString("ZoneSettingsX", pantone647cvalues); xmlWriter.WriteEndElement(); - this would write the XML Data for this value (it was based on an if conditional). Your dictionary idea grouped my elements by its xml_filename - but how can i iterate over its other fields ? i.e: i have 5 colors for one filename. – Pablo Costa Feb 25 '16 at 03:43
  • Can you post an example of your XML file content so I have more clear understand. I see you don't have right to edit question yet, so post it as an answer. – NoName Feb 25 '16 at 03:47