我一直试图理解这几个小时但没有运气。 我有一个基于班级的列表:
public class xmldata //Class to receive items list
{
public string xml_filename { get; set; }
public string colorname { get; set; }
public string colorvalues { get; set; }
}
然后我创建了一个单独的类来保存列表:
public class xmldatalist
{
public List<xmldata> FullList = new List<xmldata>();
}
此列表的每个位置(FullList)包含xmldata中的三个元素 - 我想要关注的特定元素是xml_filename。
我的问题是:如何使用foreach循环迭代此列表,并为每个xml_filename值生成一个XML文件。
即: 从位置0到5,xml_filename是file1.bla 从位置6到9,xml_filename是file2.bla
我想要一个将通过位置0到5迭代的foreach,检查xml_filename是否为file1.bla并写入位置0到5的属性。
类似:
检查xml_filename 创建文件 写属性 如果xml_filename在下一个位置是相同的 继续写这个文件 如果下一个位置的xml_file不同,请写入新文件
现在我正试图通过这样做来使用Linq:
foreach (var ListXmlFileName in XMLList.FullList.Where(x => x.xml_filename == XmlFilename))
{
}
但是我对如何继续有点失落。 我尝试的另一种方法是:
foreach (xmldata XmlStuff in XmlItems.ItemsList)
{
}
也没有成功。编写XML的代码是:
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");
我也试过这个但是没有按预期工作(while循环)
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();
}
}
任何帮助都将受到高度赞赏。非常感谢。
**
**。
<?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>
这会更准确:
<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"/>
假设这属于file1。 File1将有五个 SomeColor和SomeValues位于列表的每个位置。
答案 0 :(得分:1)
这就是你想要的:
[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);
}
}
这将创建此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>
修改强>
根据您的评论,您还有其他问题。现在解决标题问题时,你应该问其他问题的新问题。不要在这里结合起来。