XML 序列化,仅将所有内容写入一个文件
本文关键字:文件 一个 序列化 XML | 更新日期: 2023-09-27 18:36:10
关于这个问题:从基于条件的列表创建 XML 文件我解决了我遇到的问题,并看到了Xml.Serialization的简单用法。
现在运行程序会写入我期望的 XML - 但它会写入所有文件中的所有数据。即:文件 1 应该有颜色 = #FF9032颜色名称 = 谁知道文件 2 应具有颜色 = #FE9034颜色名称 = 有人知道
当程序运行时,他也从文件2写入文件1信息。基本上,如果我有 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);
}
}
}
编辑:代码已添加 - 现在上课的所有内容
EDIT2:由于属性是固定的,我在类上定义了它们。
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);
}
}
}
更改:
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;}
}