Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to pass data from the database to pass into XML node. The data I am calling, has two columns (title, body) and will always have more than 2 or more rows. The method i have created below, is outputting a null result, on the client-side.

Would I require a for loop before the XML code? Please advice further, if possible.
public static string createXML()
       {

           string xml = "";
           string title = "";
           string content = "";

           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["########"].ConnectionString);

           string commandtext = "######################";
           SqlCommand command = new SqlCommand(commandtext, con);
           con.Open();
           command.Parameters.Add(new SqlParameter("title", title));
           command.Parameters.Add(new SqlParameter("Body", content));

           SqlDataReader reader = command.ExecuteReader();

           while (reader.Read())
           {
               string top = reader.GetString(0);
               string body = reader.GetString(1);

               var result = top + "<br />" + body + "<br />";

               XElement xeRoot = new XElement("Start");
               XElement xeSendTo = new XElement("properties");
               XElement xeSendTo1 = new XElement("property");
               xeSendTo1.Add(new XAttribute("name", "Main Content"));
               xeSendTo1.Add(new XAttribute("type", "TEXT"));
               xeSendTo1.SetElementValue("value", result);//top & body parameters inside value.
               xeSendTo.Add(xeSendTo1);
               xeRoot.Add(xeSendTo);
               XDocument xDoc = new XDocument(xeRoot);
               xDoc.ToString();
           }

           return xml;
       }

Any hints to where I may be going wrong, would be very much appreciated. Thank you for your time and help.
My desired output, as the following below:
XML
<Start>
<properties>
<property name="Main" type="TEXT">
<value>
Title
content

title
content

</value>
</property>
</properties>
</Start>
Posted
Comments
george4986 8-Oct-14 23:50pm    
if u have more than one rows then it will be a table having two columns and multiple rows right? why dont u convert the datatable to xml?

Looking at your function createXML, the return value xml is not being populated.
The createXML is returning a string so you do not need to use XDocument.

The while loop needs modifying to get the result only, you could do something like this:
C#
string top = "";
string body = "";
string result = "";
while (reader.Read()){
    top = reader.GetString(0);
    body = reader.GetString(1);
    //check to make sure CarriageReturn LineFeed is working, should not use <br/> tag for this
    result = result + top + "\r\n" + body + "\r\n";
}
//create the xml string
xml = "<Start><properties><property name='Main' type='TEXT'><value>" + result + "</value></property></properties></Start>"

//To cater for tags and xml entities within the result it can be wrapped with cdata (characters the xml parser should not parse) like so
result = "<![CDATA[" + result + "]]>";

//The xml string can be parsed using the XDocument
XDocument xDoc = new XDocument();
xDoc = XDocument.Parse(xml);
//This will ensure the xml is well formed - not sure if this is a good way of doing it, or if using the creating XElement etc method below is better.

If you want to use the XDocument then use the same while loop above to get result and modify how the xml string is generated:
C#
//Move the xml stuff outside of the loop
XElement xeRoot = new XElement("Start");
XElement xeSendTo = new XElement("properties");
XElement xeSendTo1 = new XElement("property");
xeSendTo1.Add(new XAttribute("name", "Main Content"));
xeSendTo1.Add(new XAttribute("type", "TEXT"));
xeSendTo1.SetElementValue("value", result);//top & body parameters inside value.
xeSendTo.Add(xeSendTo1);
xeRoot.Add(xeSendTo);
XDocument xDoc = new XDocument(xeRoot);
//remember to give xml a value
xml = xDoc.ToString();

You should also consider changing the xml structure to add extra elements for title and content, something like this:
XML
<Start>
    <properties>
        <property name="Main" type="TEXT">
            <value>
                <title>Title</title>
                <content>content</content>
            </value>
            <value>
                <title>Title</title>
                <content>content</content>
            </value>
        </property>
    </properties>
</Start>

Hope that helps out.
I have not tested and assume your general code to get the data is working.
 
Share this answer
 
v3
Comments
Richard Deeming 8-Oct-14 12:10pm    
The benefit of using the XDocument over simple string concatenation is that it will ensure the value is properly encoded.

For example, if the field from the database contains the string "One & two & <three>", string concatenation would produce an invalid XML document.
jaket-cp 8-Oct-14 12:24pm    
yep, valid point.
string connetionString = null;
SqlConnection connection );
SqlDataAdapter adapter ;
DataSet ds = new DataSet();
string sql = null;

connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";
connection = new SqlConnection(connetionString);
sql = "select * from Product";

connection.Open();
adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(ds);
connection.Close();
ds.WriteXml("Product.xml");
MessageBox.Show("Done");
 
Share this answer
 
Thank you all, for your help and reply. I used Member 10454138's suggestion to create working solution, as shown below:
C#
SqlDataReader reader = command.ExecuteReader();

            XElement xeRoot = new XElement("start");         
            XElement xeSendTo = new XElement("properties");
            XElement xeSendTo1 = new XElement("property");
            xeSendTo1.Add(new XAttribute("name", "Main Content"));
            xeSendTo1.Add(new XAttribute("type", "TEXT"));
            xeSendTo.Add(xeSendTo1);
            xeRoot.Add(xeSendTo);
            XDocument xDoc = new XDocument(xeRoot);

            while (reader.Read())
            {
                string top = reader.GetString(0);
                string body = reader.GetString(1);

                var result = top + "<br />" + body + "<br />";

                string data = result.ToString();          
                xeSendTo1.Add(new XElement("value", (new XCData(data))));     
            }                      
            return xml = xDoc.ToString(); 


Thank you all, for your kind input into this thread. I appreciate your time and help. =D
 
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