C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




XML


Posted Date: 09 Aug 2008      Total Responses: 3

Posted By: lakshmi       Member Level: Silver     Points: 1


I am havin a xml File like this
<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
<Table>
<datasource>SqlClient</datasource>
<database></database>
<password>sa</password>
<username>sa</username>
</Table>
</NewDataSet>

IN this xml file i have to insert the value of database dynamically.

How to insert this value dynamically,into xml




Responses

Author: Saravanan Somasundaram    09 Aug 2008Member Level: SilverRating:     Points: 3
get the xml in to dataset using the readxml() method.
and then u can change the database column and save it as the xml file using the getxml() method.


Author: ANIL PANDEY    13 Aug 2008Member Level: DiamondRating:     Points: 6
hi,

please Refer the code for Writing XML

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

using BLL;

namespace DAL
{
public class DAL_XMLView
{
string strConnStr = string.Empty;

public DAL_XMLView()
{
strConnStr = Settings1.Default.SqlConnectionString.ToString();
}

#region "Member Functions"

// Function for Getting the XML Document For View.
public XmlDocument get_XMLDocument(string strMode, int lintID, int intSession)
{
BLL_Issue BobjIssue = new BLL_Issue();
BLL_Author BobjAuthor = new BLL_Author();
BLL_Search BobjSearch = new BLL_Search();
BLL_Resource BobjRes = new BLL_Resource();

int intCounter;
int lintIssue;
string strTitle;
string strTopic;
string strAuthor;
string strFname;
string strLname;
string strSummary;
string strFileContent;
string strAff;
string strText;
string strID;
string strServerIP;
char[] chrArray = { (char)34 };
string strQuote = chrArray.ToString();
DateTime dtIDate;

DataSet lds = new DataSet();
DataSet ldsIssue = new DataSet();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlDocument xmlDoc = new XmlDocument();

//Getting the ServerIP
strServerIP = BobjRes.GetServerIP();

//Getting the IssueId or ArticleId
if (lintID == 0)
{
string ID;
ID = Convert.ToString((SqlHelper.ExecuteScalar(strConnStr, CommandType.Text, "select top 1 ID from tbIssues where IsArchive='true' order by issueDate desc ")));
if (ID != "")
{
lintID = Convert.ToInt32(ID.ToString());
}
else
{
return xmlDoc;
}
}

if ((strMode == "Preview") || (strMode == "Home") || (strMode == "Current"))
{
lds = BobjIssue.getIssueById(lintID);
ldsIssue = BobjIssue.ShowEditData(lintID);
}
else if (strMode == "Article")
{
lds = BobjSearch.getArticleById(lintID);
lintIssue = Convert.ToInt32(SqlHelper.ExecuteScalar(strConnStr, CommandType.Text, "select IssueId from tbArticles where Id=" + lintID + " ").ToString());
ldsIssue = BobjIssue.ShowEditData(lintIssue);
}

XmlTextWriter tw = new XmlTextWriter(sw);
tw.Formatting = Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument();
tw.WriteStartElement("issue");
tw.WriteAttributeString("issueid", ldsIssue.Tables[0].Rows[0]["ID"].ToString());
tw.WriteAttributeString("issueno", ldsIssue.Tables[0].Rows[0]["IssueNo"].ToString());
tw.WriteAttributeString("volume", ldsIssue.Tables[0].Rows[0]["VolumeNo"].ToString());
dtIDate = Convert.ToDateTime(ldsIssue.Tables[0].Rows[0]["IssueDate"].ToString());
tw.WriteAttributeString("date", dtIDate.ToString("MMMM d, yyyy"));
tw.WriteAttributeString("archive", ldsIssue.Tables[0].Rows[0]["IsArchive"].ToString());

if (ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString().Trim() != "")
{
if (intSession != 0)
{
tw.WriteAttributeString("urlpath", "/uploads/Issue/" + ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString());
}
else
{
tw.WriteAttributeString("urlpath", "UserPages/userlogin.aspx");
}
}
else
{
if (intSession != 0)
{
tw.WriteAttributeString("urlpath", "UserPages/printarticle.aspx?pt=" + lintID);
}
else
{
tw.WriteAttributeString("urlpath", "UserPages/userlogin.aspx");
}
}

if (strServerIP != "")
{
tw.WriteAttributeString("serverip", strServerIP);
}
tw.WriteStartElement("articles");
for (intCounter = 0; intCounter < lds.Tables[0].Rows.Count; intCounter++)
{
strFileContent = lds.Tables[0].Rows[intCounter]["Contents"].ToString();
string title = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(strFileContent));
XmlNodeList articleList = doc.GetElementsByTagName("article");
tw.WriteStartElement("article");

foreach (XmlNode node in articleList)
{
XmlElement articleElement = (XmlElement)node;

strID = articleElement.GetElementsByTagName("id")[0].InnerText;
tw.WriteAttributeString("id", strID);

strTopic = articleElement.GetElementsByTagName("topic")[0].InnerText;
tw.WriteElementString("topic", strTopic);

strTitle = articleElement.GetElementsByTagName("title")[0].InnerText;
tw.WriteElementString("title", strTitle);

if (strMode != "Current")
{
tw.WriteStartElement("authors");
XmlNodeList authorList = doc.GetElementsByTagName("authors");
foreach (XmlNode node1 in authorList)
{
tw.WriteStartElement("author");
XmlElement authorElement = (XmlElement)node1;

strFname = authorElement.GetElementsByTagName("fname")[0].InnerText;
tw.WriteElementString("fname", strFname);

strLname = authorElement.GetElementsByTagName("lname")[0].InnerText;
tw.WriteElementString("lname", strLname);

strAuthor = authorElement.GetElementsByTagName("suffix")[0].InnerText;

if (strAuthor != "")
{
tw.WriteElementString("suffix", strAuthor);
}

strAff = authorElement.GetElementsByTagName("affiliation")[0].InnerText;

tw.WriteElementString("affiliation", strAff);
tw.WriteEndElement();
}
tw.WriteEndElement();
if ((strMode == "Preview") || (strMode == "Home"))
{
strSummary = articleElement.GetElementsByTagName("summary")[0].InnerText;
tw.WriteElementString("summary", strSummary);
}
else if (strMode == "Article")
{
strSummary = articleElement.GetElementsByTagName("summary")[0].InnerText;
tw.WriteElementString("summary", strSummary);

strText = articleElement.GetElementsByTagName("text")[0].InnerText;
tw.WriteElementString("text", strText);
}
}
tw.WriteEndElement();
}
}
tw.WriteEndElement();
tw.WriteEndElement();
tw.Close();
tw.Flush();
xmlDoc.LoadXml(sb.ToString());
return xmlDoc;
}

public XmlDocument get_AllArticle(int lintID)
{
BLL_Issue BobjIssue = new BLL_Issue();
BLL_Author BobjAuthor = new BLL_Author();
BLL_Search BobjSearch = new BLL_Search();
BLL_Resource BobjRes = new BLL_Resource();

int intCounter;
string strTitle;
string strTopic;
string strAuthor;
string strFname;
string strLname;
string strSummary;
string strFileContent;
string strAff;
string strText;
string strID;
string strServerIP;
char[] chrArray = { (char)34 };
string strQuote = chrArray.ToString();
DateTime dtIDate;

DataSet lds = new DataSet();
DataSet ldsIssue = new DataSet();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlDocument xmlDoc = new XmlDocument();

//Getting the ServerIP
strServerIP = BobjRes.GetServerIP();

//Getting the IssueId or ArticleId
if (lintID == 0)
{
string ID;
ID = Convert.ToString((SqlHelper.ExecuteScalar(strConnStr, CommandType.Text, "select top 1 ID from tbIssues where IsArchive='true' order by issueDate desc ")));
lintID = Convert.ToInt32(ID.ToString());
}

lds = SqlHelper.ExecuteDataset(strConnStr, CommandType.Text, "select * from tbArticles where IssueId=" + lintID + " order by ArticleOrderNo");//BobjSearch.getArticleById(lintID);
ldsIssue = SqlHelper.ExecuteDataset(strConnStr, CommandType.Text, "select ID,VolumeNo,IssueNo,convert(VARCHAR, IssueDate,107) as IssueDate,PDFFileName,IsArchive from tbIssues where ID=" + lintID);//Convert.ToInt32(.ToString());

XmlTextWriter tw = new XmlTextWriter(sw);
tw.Formatting = Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument();
tw.WriteStartElement("issue");
tw.WriteAttributeString("issueid", ldsIssue.Tables[0].Rows[0]["ID"].ToString());
tw.WriteAttributeString("issueno", ldsIssue.Tables[0].Rows[0]["IssueNo"].ToString());
tw.WriteAttributeString("volume", ldsIssue.Tables[0].Rows[0]["VolumeNo"].ToString());
dtIDate = Convert.ToDateTime(ldsIssue.Tables[0].Rows[0]["IssueDate"].ToString());
tw.WriteAttributeString("date", dtIDate.ToString("MMMM d, yyyy"));
tw.WriteAttributeString("archive", ldsIssue.Tables[0].Rows[0]["IsArchive"].ToString());
if (ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString().Trim() != "")
{
tw.WriteAttributeString("urlpath", "../uploads/Issue/" + ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString());
}
else
{
tw.WriteAttributeString("urlpath", "UserPages/printarticle.aspx?pt=" + lintID);
}

if (strServerIP != "")
{
tw.WriteAttributeString("serverip", strServerIP);
}

tw.WriteStartElement("articles");
for (intCounter = 0; intCounter < lds.Tables[0].Rows.Count; intCounter++)
{
strFileContent = lds.Tables[0].Rows[intCounter]["Contents"].ToString();
string title = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(strFileContent));
XmlNodeList articleList = doc.GetElementsByTagName("article");
tw.WriteStartElement("article");

foreach (XmlNode node in articleList)
{
XmlElement articleElement = (XmlElement)node;

strID = articleElement.GetElementsByTagName("id")[0].InnerText;
tw.WriteAttributeString("id", strID);

strTopic = articleElement.GetElementsByTagName("topic")[0].InnerText;
tw.WriteElementString("topic", strTopic);

strTitle = articleElement.GetElementsByTagName("title")[0].InnerText;
tw.WriteElementString("title", strTitle);

tw.WriteStartElement("authors");
XmlNodeList authorList = doc.GetElementsByTagName("authors");
foreach (XmlNode node1 in authorList)
{
tw.WriteStartElement("author");
XmlElement authorElement = (XmlElement)node1;

strFname = authorElement.GetElementsByTagName("fname")[0].InnerText;
tw.WriteElementString("fname", strFname);

strLname = authorElement.GetElementsByTagName("lname")[0].InnerText;
tw.WriteElementString("lname", strLname);

strAuthor = authorElement.GetElementsByTagName("suffix")[0].InnerText;

if (strAuthor != "")
{
tw.WriteElementString("suffix", strAuthor);
}

strAff = authorElement.GetElementsByTagName("affiliation")[0].InnerText;

tw.WriteElementString("affiliation", strAff);
tw.WriteEndElement();
}
tw.WriteEndElement();

strSummary = articleElement.GetElementsByTagName("summary")[0].InnerText;
tw.WriteElementString("summary", strSummary);

strText = articleElement.GetElementsByTagName("text")[0].InnerText;
tw.WriteElementString("text", strText);
tw.WriteEndElement();
}
}

tw.WriteEndElement();
tw.WriteEndElement();
tw.Close();
tw.Flush();
xmlDoc.LoadXml(sb.ToString());
return xmlDoc;
}

// Function for Getting the XML Document For PDF Generation.
public XmlDocument get_AllArticleForPDF(int lintID)
{
BLL_Issue BobjIssue = new BLL_Issue();
BLL_Author BobjAuthor = new BLL_Author();
BLL_Search BobjSearch = new BLL_Search();
BLL_Resource BobjRes = new BLL_Resource();

int intCounter;
string strTitle;
string strTopic;
string strAuthor;
string strFname;
string strLname;
string strFileContent;
string strAff;
string strText;
string strID;
string strServerIP;
char[] chrArray = { (char)34 };
string strQuote = chrArray.ToString();
DateTime dtIDate;

DataSet lds = new DataSet();
DataSet ldsIssue = new DataSet();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlDocument xmlDoc = new XmlDocument();

//Getting the ServerIP
strServerIP = BobjRes.GetServerIP();

//Getting the IssueId or ArticleId
if (lintID == 0)
{
string ID;
ID = Convert.ToString((SqlHelper.ExecuteScalar(strConnStr, CommandType.Text, "select top 1 ID from dbo.tbIssues where IsArchive='true' order by issueDate desc ")));
lintID = Convert.ToInt32(ID.ToString());
}
lds = SqlHelper.ExecuteDataset(strConnStr, CommandType.Text, "select ID,IssueId,Contents,ArticleOrderNo,ModifiedBy,ModifiedDate from dbo.tbArticles where IssueId=" + lintID + " order by ArticleOrderNo");//BobjSearch.getArticleById(lintID);
ldsIssue = SqlHelper.ExecuteDataset(strConnStr, CommandType.Text, "select ID,VolumeNo,IssueNo,convert(VARCHAR, IssueDate,107) as IssueDate,PDFFileName,IsArchive from dbo.tbIssues where ID=" + lintID);//Convert.ToInt32(.ToString());

XmlTextWriter tw = new XmlTextWriter(sw);
tw.Formatting = Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument();
tw.WriteStartElement("issue");
tw.WriteAttributeString("issue", ldsIssue.Tables[0].Rows[0]["ID"].ToString());
tw.WriteAttributeString("issueno", ldsIssue.Tables[0].Rows[0]["IssueNo"].ToString());
tw.WriteAttributeString("volume", ldsIssue.Tables[0].Rows[0]["VolumeNo"].ToString());
dtIDate = Convert.ToDateTime(ldsIssue.Tables[0].Rows[0]["IssueDate"].ToString());
tw.WriteAttributeString("date", dtIDate.ToString("MMMM d, yyyy"));
tw.WriteAttributeString("archive", ldsIssue.Tables[0].Rows[0]["IsArchive"].ToString());
if (ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString().Trim() != "")
{
tw.WriteAttributeString("urlpath", "../uploads/Issue/" + ldsIssue.Tables[0].Rows[0]["PDFFileName"].ToString());
}
else
{
tw.WriteAttributeString("urlpath", "UserPages/printarticle.aspx?pt=" + lintID);
}

if (strServerIP != "")
{
tw.WriteAttributeString("serverip", strServerIP);
}

tw.WriteStartElement("articles");
int intTopicSrNo = 0;
for (intCounter = 0; intCounter < lds.Tables[0].Rows.Count; intCounter++)
{
strFileContent = lds.Tables[0].Rows[intCounter]["Contents"].ToString();
string title = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(strFileContent));
XmlNodeList articleList = doc.GetElementsByTagName("article");
tw.WriteStartElement("article");


intTopicSrNo++;
foreach (XmlNode node in articleList)
{
XmlElement articleElement = (XmlElement)node;

strID = articleElement.GetElementsByTagName("id")[0].InnerText;
tw.WriteAttributeString("id", strID);

strTopic = articleElement.GetElementsByTagName("topic")[0].InnerText;
//tw.WriteElementString("topic", strTopic);

tw.WriteStartElement("topic");
tw.WriteAttributeString("id",Convert.ToString(intTopicSrNo));
tw.WriteString(strTopic);
tw.WriteEndElement();

strTitle = articleElement.GetElementsByTagName("title")[0].InnerText;
tw.WriteElementString("title", strTitle);

XmlNodeList authorList = doc.GetElementsByTagName("authors");
string strAuthorFullName = string.Empty;
foreach (XmlNode node1 in authorList)
{
XmlElement authorElement = (XmlElement)node1;
strFname = authorElement.GetElementsByTagName("fname")[0].InnerText;
strLname = authorElement.GetElementsByTagName("lname")[0].InnerText;
strAuthor = authorElement.GetElementsByTagName("suffix")[0].InnerText;
strAff = authorElement.GetElementsByTagName("affiliation")[0].InnerText;
strAuthorFullName = "";

if (strAuthor != "")
{
strAuthorFullName = strFname + " " + strLname + " " + strAuthor;
}
else
{
strAuthorFullName = strFname + " " + strLname;
}
if (strAuthorFullName.Trim() == "")
{
tw.WriteElementString("authors", "");
}
else
{
tw.WriteElementString("authors", strAuthorFullName);
}
tw.WriteElementString("affiliations","");
}
strText = articleElement.GetElementsByTagName("text")[0].InnerText;
tw.WriteElementString("fulltext", strText);
tw.WriteEndElement();
}
}

tw.WriteEndElement();
tw.WriteEndElement();
tw.Close();
tw.Flush();
xmlDoc.LoadXml(sb.ToString());
return xmlDoc;
}
#endregion
}
}

Regards
Anil



Author: Sabu C Alex    20 Aug 2008Member Level: GoldRating:     Points: 5
Hai Subbalakshmi

see this function in c#

private void EditXmlFile()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"E:\Test.xml");
XmlNode xmlNde = doc.DocumentElement.SelectSingleNode(@"Table");
XmlNode databaseNode = xmlNde.SelectSingleNode(@"database");
databaseNode.InnerText = "Pubs";
doc.Save(@"E:\Test.xml");
}

i think this will help you
Sabu


Post Reply
You must Sign In to post a response.
Next : Implementing RSS feed in Asp.Net
Previous : How to apply permissions for registry key in WIX installer
Return to Discussion Forum
Post New Message
Category: XML

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

it outsourcing

Contact Us    Privacy Policy    Terms Of Use