关于这个问题: Create a XML File from a list based on a conditional 我解决了我遇到的问题,并看到了一个简单的Xml.Serialization使用。
现在运行程序会编写我期望的XML - 但它会将所有数据写入所有文件中。 即: file1应该有 color =#FF9032 colorname = WhoKnows 文件2应该有 color =#FE9034 colorname = SomeoneKnows
当程序运行时,他也会从file2写入file1信息。基本上,如果我要处理8个文件,他将从8个文件的8个文件中写下每个信息(颜色和颜色名称)。
此行为来自何处以及如何解决?
非常感谢任何帮助。感谢。
XML编写代码(在上一个问题中可以找到其他任何内容)。
public void writexml(xmldatalist XMLList, variables GlobalVars)
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t",
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace,
Encoding = new UTF8Encoding(false)
};
foreach(var item in XMLList.FullList)
{
if (!XMLList.xmlFiles.ContainsKey(item.xml_filename))
{
XMLList.xmlFiles[item.xml_filename] = new List<xmldata>();
XMLList.xmlFiles[item.xml_filename].Add(item);
}
}
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string XmlFilename = GlobalVars.CompleteFileName;
XmlFilename = GlobalVars.CompleteFileName;
string FileExtension = ".xml";
string PathString = Path.Combine(DesktopFolder, "XML");
System.IO.Directory.CreateDirectory(PathString);
string FullPath = Path.Combine(PathString, XmlFilename + FileExtension);
foreach (var i in XMLList.xmlFiles)
{
string Xname = i.Key;
string XMLName = Path.Combine(PathString, Xname + FileExtension);
List<xmldata> xmlDataOfThisFile = i.Value;
Console.WriteLine(Xname);
try
{
using (XmlWriter XmlWriting = XmlWriter.Create(XMLName, 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");
XmlSerializer serializer = new XmlSerializer(typeof(List<xmldata>));
serializer.Serialize(XmlWriting, XMLList.FullList);
XmlWriting.WriteEndElement();
XmlWriting.WriteEndDocument();
XmlWriting.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
}
}
}
namespace PPF_Converter_v6
{
[XmlRoot("JMF",Namespace = "http://www.CIP4.org/JDFSchema_1_1")]
public class JMF
{
[XmlElement("SenderID")]
public string SenderID { get; set; }
[XmlElement("Version")]
public string Version { get; set; }
public JMF()
{
SenderID = "InkZone-Controller";
Version = "1.2";
}
}
[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; }
}
[Serializable]
public class Command
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("Type")]
public string Type { get; set; }
public Command()
{
ID = "cmd.00695";
Type = "Resource";
}
public ResourceCmdParams ResourceCmdParams { get; set; }
}
[Serializable]
public class ResourceCmdParams
{
[XmlAttribute("ResourceName")]
public string ResourceName { get; set; }
[XmlAttribute("JobID")]
public string JobID { get; set; }
public ResourceCmdParams()
{
ResourceName = "InkZoneProfile";
JobID = "K_41";
}
public InkZoneProfile InkZoneProfile { get; set; }
}
[Serializable]
public class InkZoneProfile
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("Class")]
public string Class { get; set; }
[XmlAttribute("Locked")]
public string Locked { get; set; }
[XmlAttribute("Status")]
public string Status { get; set; }
[XmlAttribute("PartIDKeys")]
public string PartIDKeys { get; set; }
[XmlAttribute("DescriptiveName")]
public string DescriptiveName { get; set; }
[XmlAttribute("ZoneWidth")]
public string ZoneWidth { get; set; }
public InkZoneProfile()
{
ID = "r0013";
Class = "Parameter";
Locked = "False";
Status = "Available";
PartIDKeys = "SignatureName SheetName Side Separation";
DescriptiveName = "Schieberwerte von DI";
ZoneWidth = "32";
}
public InkZoneProfile2 InkZoneProfile2 { get; set; }
}
public class InkZoneProfile2 //Since InkZoneProfile was in use for class name, i just incremented it. Default name still is InkZoneProfile
{
[XmlAttribute("SignatureName")]
public string SignatureName { get; set; }
public InkZoneProfile2()
{
SignatureName = "SIG1";
}
public InkZoneProfile3 InkZoneProfile3 { get; set; }
}
public class InkZoneProfile3
{
[XmlAttribute("Locked")]
public string Locked { get; set; }
[XmlAttribute("SheetName")]
public string SheetName { get; set; }
public InkZoneProfile3()
{
Locked = "False";
SheetName = "S1";
}
}
public class xmldatalist
{
public List<xmldata> FullList = new List<xmldata>();
public Dictionary<string, List<xmldata>> xmlFiles = new Dictionary<string, List<xmldata>>();
}
public class variables //All variables goes here - only smaller stuff goes into the code
{
public string aux { get; set; }
public string colors { get; set; }
public string[] colors_str { get; set; }
public int nfiles { get; set; }
public string definedpath { get; set; }
public string contents { get; set; }
public string values { get; set; }
public string CompleteFileName { get; set; }
public string[] FilesNames { get; set; }
}
class Program
{
public void writexml(xmldatalist XMLList, variables GlobalVars)
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t",
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace,
Encoding = new UTF8Encoding(false)
};
foreach (var item in XMLList.FullList)
{
if (!XMLList.xmlFiles.ContainsKey(item.xml_filename))
{
XMLList.xmlFiles[item.xml_filename] = new List<xmldata>();
XMLList.xmlFiles[item.xml_filename].Add(item);
}
}
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string XmlFilename = GlobalVars.CompleteFileName;
XmlFilename = GlobalVars.CompleteFileName;
string FileExtension = ".xml";
string PathString = Path.Combine(DesktopFolder, "XML");
System.IO.Directory.CreateDirectory(PathString);
string FullPath = Path.Combine(PathString, XmlFilename + FileExtension);
foreach (var i in XMLList.xmlFiles)
{
string Xname = i.Key;
string XMLName = Path.Combine(PathString, Xname + FileExtension);
List<xmldata> xmlDataOfThisFile = i.Value;
Console.WriteLine(Xname);
try
{
using (XmlWriter XmlWriting = XmlWriter.Create(XMLName, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("myNamespace", "http://www.CIP4.org/JDFSchema_1_1");
XmlSerializer serializer = new XmlSerializer(typeof(List<xmldata>));
serializer.Serialize(Console.Out, xmlDataOfThisFile);
XmlWriting.WriteEndElement();
XmlWriting.WriteEndDocument();
XmlWriting.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
}
}
}
答案 0 :(得分:1)
变化:
serializer.Serialize(XmlWriting, XMLList.FullList);
要:
serializer.Serialize(XmlWriting, xmlDataOfThisFile);
MSDN很好,也可以在stackoverflow.com和codeproject.com上搜索
如果您可以将上述代码转换为课程,我会自己帮助您。
您需要将这些内容转换为课程:JMF
Command
ResourceCmdParams
,...
示例:
[Serializable]
public class JMF
{
[XmlAttribute("SenderID")]
public string SenderID { get; set; }
[XmlAttribute("Version")]
public string Version { get; set; }
public Command Command { get; set; }
}
[Serializable]
public class Command
{
[XmlAttribute("ID")]
public string ID{get;set;}
[XmlAttribute("Type")]
public string Type { get; set; }
public ResourceCmdParams {get; set;}
}