0

I have been writing some code to popular a word document through the content controls with XML mapping using open xml format sdk 2.0. I have an two dimensional array which I want to loop through and place each par of the information on a new line on the document;

public static string[,] ArrSkills = new string[,] {{"Live", "The Dream", "More"}, {"And", "Even", "More"} };

I have two functions one which maps the XML and the other with is a list of the xml to loop through

 #region XMLElements
    public static string XMLName = "<Name>" + ArrPName[0] + "</Name>";
    public static string XMLAddress = "<Address>"+ AArrddress[0] +"</Address>";
    public static string XMLHomeNumber = "<HomeNumber>"+ ArrHomePhoneNunber[0] +"</HomeNumber>";
    public static string XMLMobileNumber = "<MobileNumber>"+ ArrMobileNumber[0] +"</MobileNumber>";
    public static string XMLEmail = "<Email>"+ ArrEmail[0] +"</Email>";
    public static string XMLDriversLicence = "<DriversLicence>"+ ArrDriversLicence[0] +"</DriversLicence>";
    public static string XMLCarOwner = "<CarOwner>"+ ArrCarOwner[0] +"</CarOwner>";
    public static string XMLPStatement = "<PStatement>"+ ArrPStatement[0] +"</PStatement>";
    public static string XMLskills = "<SkillsAndQualities>"+ XMLskills +"</SkillsAndQualities>";
    public static string XMLEd = "<ed> </ed>";
    public static string XMLWork = "<WorkEx>"+ ArrWork[0] +"</WorkEx>";
    public static string XMLInterest = "<InterestAndHobbies>"+ ArrInterest[0] +"</InterestAndHobbies>";
    public static string XMLRef = "<Ref>" + ArrReferences[0] + "</Ref>";
    #endregion

    public static void WriteWordFile(string TemplatePath)
    {
        using (WordprocessingDocument WPDoc = WordprocessingDocument.Open(TemplatePath, true))
        {

            SortElements(TemplatePath);

            string XML = "<root>" +
                         XMLName +
                         XMLAddress +
                         XMLHomeNumber +
                         XMLMobileNumber +
                         XMLEmail +
                         XMLDriversLicence +
                         XMLCarOwner +
                         XMLPStatement +
                         XMLskills +
                         XMLEd +
                         XMLWork +
                         XMLInterest +
                         XMLRef +
                         "</root>";

            MainDocumentPart MainDoc = WPDoc.MainDocumentPart;
            MainDoc.DeleteParts<CustomXmlPart>(MainDoc.CustomXmlParts);

            CustomXmlPart CustomXML = MainDoc.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            using (StreamWriter SW = new StreamWriter(CustomXML.GetStream()))
            {
                SW.Write(XML.ToString());
            }
        }
    }

    public static List<string> SortElements(string TemplatePath)
    {
        List<string> ElementsList = new List<string>();
        ElementsList.Add(XMLName);
        ElementsList.Add(XMLAddress);
        ElementsList.Add(XMLHomeNumber);
        ElementsList.Add(XMLMobileNumber);
        ElementsList.Add(XMLEmail);
        ElementsList.Add(XMLDriversLicence);
        ElementsList.Add(XMLCarOwner);
        ElementsList.Add(XMLPStatement);
        ElementsList.Add(XMLskills);
        ElementsList.Add(XMLEd);
        ElementsList.Add(XMLWork);
        ElementsList.Add(XMLInterest);
        ElementsList.Add(XMLRef);

        foreach (var FindItem in ElementsList)
        {
            if (FindItem.Equals(XMLskills))
            {
                for (int i = 0; i < ArrSkills.GetLength(0); i++)
                {
                    string NewLine = ArrSkills[i, 0];

                    NewLine = XMLskills;
                }
            }
            else if (FindItem.Equals(XMLWork))
            {

            }
        }

        return ElementsList;
    }

The loop checks the list of xml items and when it gets to match "Skills" it then loops through the array within the if statement.

The issue I am finding when I am iterate the array it is coming up with the end character of first part of the array "More" and print just "e" out into the document. I was just wondering who would you loop through the array and get all the information from it onto the word document

willa
  • 629
  • 1
  • 9
  • 17

1 Answers1

0
  public static List<string> SortElements(string TemplatePath)
  {
     List<string> ElementsList = new List<string>();
     ElementsList.Add(XMLName);
     ElementsList.Add(XMLAddress);
     ElementsList.Add(XMLHomeNumber);
     ElementsList.Add(XMLMobileNumber);
     ElementsList.Add(XMLEmail);
     ElementsList.Add(XMLDriversLicence);
     ElementsList.Add(XMLCarOwner);
     ElementsList.Add(XMLPStatement);

     string SkillsAndQuantitiesElement = "<SkillsAndQualities>";
     for (int i = 0; i < ArrSkills.GetLength(0); i++)
     {
        for (int j = 0; j < ArrSkills.GetLength(1); j++)
        {
           if (i == 0)
           {
              SkillsAndQuantitiesElement += "<Skill>" + ArrSkills[i, j] + "</Skill>";
           }
           else
           {
              SkillsAndQuantitiesElement += "<Quality>" + ArrSkills[i, j] + "</Quality>";
           }
        }
     }
     SkillsAndQuantitiesElement += "</SkillsAndQualities>";

     ElementsList.Add(SkillsAndQuantitiesElement);

     ElementsList.Add(XMLEd);
     ElementsList.Add(XMLWork);
     ElementsList.Add(XMLInterest);
     ElementsList.Add(XMLRef);

     return ElementsList;
  }
LVBen
  • 2,041
  • 13
  • 27