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:
29 Aug 2008
Total Responses:
3
|
Posted By: Siva Sankari.R Member Level: Gold Points: 1
|
Hi,
Iam new to XML. Still now i did my home projects using ASP.NET2.0 and C#.NET. i need to study XML now. can any one the tutorial and also how it is connected to ASP.NET2.0 with a sample code.
Regards, Siva
Regards Siva Sankari.R
|
Responses
|
| Author: ANIL PANDEY 29 Aug 2008 | Member Level: Diamond | Rating: Points: 6 | hi,
u can try this sample code
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 } }
Thanks Anil
Thanks & Regards Anil Kumar Pandey
| | Author: Legend 29 Aug 2008 | Member Level: Silver | Rating: Points: 6 | Here is XML Tutorial Link
http://www.w3schools.com/xml/default.asp and http://www.w3schools.com/XML/xml_examples.asp
OR
this is the simple xml file
<?xml version="1.0" encoding="ISO-8859-1" ?> - <!-- Edited by XMLSpy® --> - <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
XML FILE with an ERROR
The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
End tag 'Ffrom' does not match the start tag 'from'. Error processing resource 'http://www.w3schools.com/XML/note_error.xml...
<from>Jani</Ffrom> -------------^
| | Author: Sreekanth M 29 Aug 2008 | Member Level: Silver | Rating: Points: 6 | Hi, If you are looking to insert data to an xml file you could have a glimpse of the below code :
XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("test39.xml")); XmlNodeList Slot = doc.GetElementsByTagName("slides"); XmlElement node = doc.CreateElement( "slideimage" );
XmlAttribute attr1 = doc.CreateAttribute("Path"); attr1.Value = "path"; // XmlAttribute attr2 = doc.CreateAttribute("name"); attr2.Value = "namename"; XmlAttribute attr3 = doc.CreateAttribute("description"); attr3.Value = "descriptiondescription"; node.SetAttributeNode(attr1); node.SetAttributeNode(attr2); node.SetAttributeNode(attr3);
try { Slot[0].AppendChild(node); } catch (Exception e1) { //no slides found } doc.Save("c:/test.xml");
an even insert an xml file into another xml file as below : 1st.xml <Document> <general-1> <rec> <line-1>aaaa</line-1> </rec> <rec> <line-2>bbbb</line-2> </rec> </general-1> <general-1> <rec> <line-1>aaaa</line-1> </rec> <rec> <line-2>bbbb</line-2> </rec> </general-1> <general-2> <rec> <line-1>cccc</line-1> </rec> <rec> <line-2>dddd</line-2> </rec> </general-2> <!-- here insert b.xml --> <general-3> <rec> <line-1>eeeee</line-1> </rec> <rec> <line-2>fffff</line-2> </rec> </general-3> </Document>
2nd.xml
<personel> <f-rec> <add1>121212</add1> <add2>222222</add2> </f-rec> <s-rec> <add1>121212</add1> <add2>222222</add2> </s-rec> </personel>
//code to insert
try{ Document aDocucment = docBuilder.parse(new File("c:\\temp\\A.xml")); Document bDocument = docBuilder.parse(new File("c:\\temp\\B.xml")); Node root = bDocument.getElementsByTagName("personel").item(0); Node root1 = aDocucment.getElementsByTagName("Document").item(0);
Node general3Node = aDocucment.getElementsByTagName("general-3").item(0); Node newNode = aDocucment.importNode(root,true);
root1.insertBefore(newNode,general3Node);
}catch(Exception e){ e.printStackTrace(); } end try
|
| Post Reply |
|
|
|
You must Sign In to post a response.
|
|
|
|