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.
**
**.
<?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.
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.