1

On this question: Create a XML File from a list based on a conditional I solved the problem i was having and saw a simple use of Xml.Serialization.

Running the program now writes the XML i expect - but it writes all data in all files. i.e: file1 should have color = #FF9032 colorname = WhoKnows file 2 should have color = #FE9034 colorname = SomeoneKnows

When the program runs he writes into file1 information from file2 also. Basically if i have 8 files to be processed, he'll write every information (color and colorname) from the 8 files on the 8 files.

Where this behaviour came from and how can i fix it ?

Any help is kindly appreciated. Thanks.

XML Writing Code(Anything else can be found in the previous question).

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);
        }
    }
}

EDIT: Code Added - Everything on classes now

EDIT2: Since the attributes were fixed i defined them on the classes.

   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);
                }
            }
        }
Community
  • 1
  • 1
Pablo Costa
  • 125
  • 4
  • 14
  • I recommend you change any `XmlWriting.WriteAttributeString` and `XmlWriting.WriteStartElement` to the class like the `xmldata` class. That way you more easily control and manage your code. – NoName Feb 25 '16 at 05:20
  • Call `JMF jmf = new JMF();` add data to `jmf` and see if it work. If it doesn't, post your code here and I will help you. – NoName Feb 25 '16 at 06:42
  • I edited the code. Based on an article i readed i defined the attributes already. – Pablo Costa Feb 25 '16 at 14:04
  • Try to serial it to file to see if it work. If it not, post your code your try I can fix for you. – NoName Feb 25 '16 at 14:07
  • I changed the output to console so its better to debug. Till now the only attribute being added is InkZoneProfile . I'm also trying to get rid of "ArrayOf" (reading about). My current code is the one on EDIT2 . – Pablo Costa Feb 25 '16 at 14:13
  • Your code is large, so it hard to follow. If you can send me you console app I will fix it for you – NoName Feb 25 '16 at 14:21
  • http://pastebin.com/KxHfiah3 - entire code is here. Just paste into a new project. Also on this link: https://drive.google.com/open?id=0BwU9_GrFRYrTT0ZTS2dRMUhIWms - there's a sample file - just make a copy of it for reading multiple files. But most important i would like to know (if possible) what i have to fix - i'm googling stuff all the time and trying also. – Pablo Costa Feb 25 '16 at 14:35
  • I believe i've found why other attributes are not being added. When i serialize i'm serializing only the List type. If data isn't in that format (like other classes) it won't work. – Pablo Costa Feb 25 '16 at 15:07
  • could you please post your code to google drive also. `pastebin` being blocked in my country. – NoName Feb 25 '16 at 15:16
  • https://drive.google.com/folderview?id=0BwU9_GrFRYrTakZsaWJUQlBIcDg&usp=sharing - Uploaded entire project folder. Just open Program.cs - everything's there. – Pablo Costa Feb 25 '16 at 15:25
  • See my updated answer. – NoName Feb 25 '16 at 15:58
  • @Sakura - thanks a lot for all the help you gave me, all your attention and effort - but in the end i returned to my initial approach and made it work. I created an array of lists. So , every position in the array is an list with all data from a specific file. Position 1 file 1, position 2 file 2 , and so goes on :) ! – Pablo Costa Feb 25 '16 at 19:21

1 Answers1

1

Change:

serializer.Serialize(XmlWriting, XMLList.FullList);

To:

serializer.Serialize(XmlWriting, xmlDataOfThisFile);

MSDN is good, also search at stackoverflow.com and codeproject.com

If you can convert above code to class, I will help you myself.

You need convert these things to class: JMF Command ResourceCmdParams,...

Example:

[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;}
}
NoName
  • 7,940
  • 13
  • 56
  • 108
  • By doing so he adds the first attribute of each file and moves to the next one. I'm trying to figure out how serialization works - you recommend any other documentation rather than MSDN ? – Pablo Costa Feb 25 '16 at 05:20
  • http://stackoverflow.com/questions/18570657/c-sharp-xml-serialization-of-derived-classes?rq=1 - something like it ? – Pablo Costa Feb 25 '16 at 05:23
  • i understood why you told me to change everything to classes - since i'm serializing the data it makes way more easier to control data input\output. I changed the way you told me but i'm completely lost about how am i going to proceed on writing the XML. I'm checking MSDN , StackOverflow and C# Corner all the time - thats how i got to this point :) – Pablo Costa Feb 25 '16 at 05:55
  • Its okay. You've did something i tested here (serialize JMF). Thanks for everything from now on i can handle\do something\figure it out. Thanks a lot. – Pablo Costa Feb 25 '16 at 16:04