Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to create a XML at run-time and the xml looks like :
XML
<?xml version=""1.0"" encoding=""utf-8""?>
<root islandLocation=""D:\Regression1\Islands"" otherFileLocation=""D:\Regression1"">
<port>" + portConfigtoBase64String + @"</port>
<setup>" + setuptoBase64String + @"</setup>
<settings>" + settingstoBase64String + @"</settings>
<island>
<name>ABC</name>
<data>" + islandtoBase64String[0] + @"</data>
</island>
<island>
<name>PQR</name>
<data>" + islandtoBase64String[1] + @"</data>
</island>
</root>"


Here the number of <island> tags is not fixed. It varies at each run. So how can i make changes to my code so that this can be implemented, i.e. the number of <island> tags be controlled.

Thanks :)
Posted

Here is a sample
C#
string[] islandtoBase64String = { "sfsgsdg", "sgtyeyewy", "rhjfjgfjkgfj", "fgjgfjgfjgfj" };
           string[] names = { "AAA", "BBB", "CCC","DDD" };

           XmlDocument doc = new XmlDocument();
           XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
           doc.AppendChild(docNode);

           XmlNode productsNode = doc.CreateElement("products");
           doc.AppendChild(productsNode);
           int cnt = 0;
           foreach (string s in names)
           {

               XmlNode productNode = doc.CreateElement("island");
               productsNode.AppendChild(productNode);

               XmlNode nameNode = doc.CreateElement("Name");
               nameNode.AppendChild(doc.CreateTextNode(s));
               productNode.AppendChild(nameNode);
               XmlNode priceNode = doc.CreateElement("data");
               priceNode.AppendChild(doc.CreateTextNode(islandtoBase64String[cnt++]));
               productNode.AppendChild(priceNode);
           }

           doc.Save(@"D:\test.xml");


Make changes as per your requirement
 
Share this answer
 
Comments
sovit agarwal 28-May-14 2:13am    
Thanks a lot...but how do i add the other parts of the XML to the above code
root islandLocation=""D:\Regression1\Islands"" otherFileLocation=""D:\Regression1"">
<port>" + portConfigtoBase64String + @"</port>
<setup>" + setuptoBase64String + @"
<settings>" + settingstoBase64String + @"
sovit agarwal 28-May-14 2:30am    
The name and data elements are created before the island tag and not inside it...
How can i change that
 
Share this answer
 
Comments
sovit agarwal 28-May-14 1:59am    
The point is i need to add variable values to my XMl, i.e. the value of the nodes are stored in varibales and that i need to insert these variables in my XML as shown above
anuradha.sardesai 28-May-14 4:29am    
You can add any characters to the xml, add escape sequences if required.
C#
public class GenerateXml {
    private static void Main() {
        string abc="wqfguiqaghekfjakjfa";
        string pqr="wgwerhgrewhgerhg";
        string xyz="rgwgewrhwgwgwgwegw";
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode productsNode = doc.CreateElement("root");
        doc.AppendChild(productsNode);

        XmlNode productNode = doc.CreateElement("FileTransfer");
        XmlAttribute productAttribute = doc.CreateAttribute("islandLocation");
        productAttribute.Value = @"D:\Regression1\Islands";
        productNode.Attributes.Append(productAttribute);
        XmlAttribute pd = doc.CreateAttribute("otherFilelocation");
        pd.Value = @"D:\Regression1";
        productNode.Attributes.Append(pd);
        productsNode.AppendChild(productNode);

        XmlNode nameNode = doc.CreateElement("Port");
        nameNode.AppendChild(doc.CreateTextNode(abc));
        productNode.AppendChild(nameNode);
        XmlNode priceNode = doc.CreateElement("setup");
        priceNode.AppendChild(doc.CreateTextNode(pqr));
        productNode.AppendChild(priceNode);
        XmlNode set = doc.CreateElement("settings");
        set.AppendChild(doc.CreateTextNode(xyz));
        productNode.AppendChild(set);
        

        string[] islandtoBase64String = { "sfsgsdg", "sgtyeyewy", "rhjfjgfjkgfj", "fgjgfjgfjgfj" };
            string[] names = { "AAA", "BBB", "CCC","DDD" };
            int cnt = 0;
            foreach (string s in names)
            {
 
                XmlNode pn1 = doc.CreateElement("island");                
                productsNode.AppendChild(pn1);
 
                XmlNode nn = doc.CreateElement("Name");
                nn.AppendChild(doc.CreateTextNode(s));
                pn1.AppendChild(nn);
                XmlNode nm= doc.CreateElement("data");
                nm.AppendChild(doc.CreateTextNode(islandtoBase64String[cnt++]));
                pn1.AppendChild(nm);
            }
            
            doc.Save(@"D:\test.xml");

        doc.Save(Console.Out);
        Console.ReadKey();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900