I recursively want to display xml nodes. But unfortunately it doesn't work. The output is only the first element of the xml file. Why?
public string GetOutline(int indentLevel, XmlNode xnod)
{
StringBuilder result = new StringBuilder();
XmlNode xnodWorking;
result = result.AppendLine(new string('-', indentLevel * 2) + xnod.Name);
if (xnod.NodeType == XmlNodeType.Element)
{
if (xnod.HasChildNodes)
{
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
GetOutline(indentLevel + 1, xnodWorking);
xnodWorking = xnodWorking.NextSibling;
}
}
}
return result.ToString();
}
Here the code calling the function. The XML file begins with <Videos> then <Video>... etc...
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog fDialog = new SaveFileDialog();
fDialog.Title = "Save XML File";
fDialog.FileName = "drzewo.xml";
fDialog.CheckFileExists = false;
fDialog.InitialDirectory = #"C:\Users\Piotrek\Desktop";
if (fDialog.ShowDialog() == DialogResult.OK)
{
using (var newXmlFile = File.Create(fDialog.FileName));
{
string xmlTree = fDialog.FileName.ToString();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(XML);
XmlNode xnodDE = xdoc.DocumentElement;
textBox2.Text = GetOutline(0, xnodDE);
//StringBuilder result = new StringBuilder();
/*
foreach (var childelement in xdoc.DescendantNodes().OfType<XElement>()
.Select(x => x.Name).Distinct())
{
result.Append(childelement + Environment.NewLine );
}
textBox2.Text = result.ToString();
*/
using (StreamWriter sw = File.AppendText(xmlTree))
{
sw.Write(textBox2.Text);
}
}
}
XML content :
<Videos>
<Video>
<Title>The Distinguished Gentleman</Title>
<Director>Jonathan Lynn</Director>
<Actors>
<Actor>Eddie Murphy</Actor>
<Actor>Lane Smith</Actor>
<Actor>Sheryl Lee Ralph</Actor>
<Actor>Joe Don Baker</Actor>
</Actors>
<Length>112 Minutes</Length>
<Format>DVD</Format>
<Rating>R</Rating>
</Video>
<Video>
<Title>Her Alibi</Title>
<Director>Bruce Beresford</Director>
<Length>94 Mins</Length>
<Format>DVD</Format>
<Rating>PG-13</Rating>
</Video>
</Videos>
You need to read all document line by line whith a for each or a while instruction
XmlReader reader = XmlReader.Create(your xml file);
reader.MoveToContent();
while (reader.Read())
{
// your code
}
reader.Close();
not the best way, try to have a look also on linq to xml
try that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace testStackOverflow
{
class Program
{
static void Main(string[] args)
{
//Load xml
XDocument xdoc = XDocument.Load("test.xml");
//Run query
var lv1s = from lv1 in xdoc.Descendants("Video")
select new
{
title = lv1.Element("Title").Value
};
//Loop through results
foreach (var lv1 in lv1s)
{
Console.WriteLine(lv1.title);
}
Console.ReadLine();
}
}
}
You're not doing anything to add the results of the recursive calls to the string you're building. You need to do this:
result.Append(GetOutline(indentLevel + 1, xnodWorking));
And this modification should avoid the text nodes and nodes with the same name:
public string GetOutline(int indentLevel, XmlNode xnod)
{
StringBuilder result = new StringBuilder();
XmlNode xnodWorking;
result = result.AppendLine(new string('-', indentLevel * 2) + xnod.Name);
if (xnod.HasChildNodes)
{
List<string> foundElements = new List<string>();
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
if(xnodworking.NodeType == XmlNodeType.Element && !foundElements.Contains(xnodworking.Name))
{
result.Append(GetOutline(indentLevel + 1, xnodWorking));
foundElements.Add(xnodworking.Name);
}
xnodWorking = xnodWorking.NextSibling;
}
}
return result.ToString();
}
Related
I'm new to C# & I'm trying to read XML attribute from XML file in my Asp.net web project, when i retrieve attribute content i get this result :
"System.XML.Attribute;"
here is XML file:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<!--creating XML file-->
<Document PagesInDocument="1" Filename="YahyaXML">
<Page Filename="YahyaXML" ZonesInPage="1" PageID="0">
<Zone TextLinesInZone="2" zoneLRY="558" zoneLRX="1202"
zoneULY="3" zoneULX="886" ZoneID="0">
<TextLine Content="aaaaaaaaaaa" TextLineID="0"/>
<TextLine Content="aaaaaaaaaaaaaaaaaa" TextLineID="1"/>
</Zone>
</Page>
</Document>
Here is my c# code:
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\Program Files (x86)\NovoDynamics\NovoVerus\api\examples\C#Samples\WebAppTest\Uploads\test-ht.xml");
StringBuilder Content_Value = new StringBuilder();
XmlNodeList aNodes = doc.SelectNodes("/Document/Page/Zone");
XmlNodeList bNodes = doc.SelectNodes("/Document/Page/Zone/TextLine");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute ULX_xml = aNode.Attributes["zoneULX"];
XmlAttribute ULY_xml = aNode.Attributes["zoneULY"];
XmlAttribute LRX_xml = aNode.Attributes["zoneLRX"];
XmlAttribute LRY_xml = aNode.Attributes["zoneLRY"];
if (ULX_xml.Value == TextBox5.Text && ULY_xml.Value == TextBox6.Text && LRX_xml.Value == TextBox7.Text && LRY_xml.Value == TextBox8.Text )
{
foreach (XmlNode bNode in bNodes)
{
//XmlAttribute ContentAttribute = bNode.Attributes["Content"];
if (bNode.Attributes["Content"].Value != null)
{
Content_Value.Append(bNode.Attributes["Content"].Value.ToString());
}
}
Content_Value.Append("\n");
}
}
TextBox9.Text = Content_Value.ToString();
Your code works. Try change first string in your xml file to:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
More: https://msdn.microsoft.com/en-us/library/ms256048(v=vs.120).aspx
Using xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
reader.ReadLine(); //skip identification
XDocument doc = XDocument.Load(reader);
var results = doc.Descendants("Page").Select(x => new
{
filename = (string)x.Attribute("Filename"),
page = (int)x.Attribute("ZonesInPage"),
id = (int)x.Attribute("PageID"),
zones = x.Elements("Zone").Select(y => new {
textLines = (int)y.Attribute("TextLinesInZone"),
ulx = (int)y.Attribute("zoneULX"),
uly = (int)y.Attribute("zoneULY"),
lrx = (int)y.Attribute("zoneLRX"),
lry = (int)y.Attribute("zoneLRY"),
textLine = y.Elements("TextLine").Select(z => new {
content = (string)z.Attribute("Content"),
id = (int)z.Attribute("TextLineID")
}).ToList()
}).ToList()
}).ToList();
}
}
}
I have some code that I need to put into a string list in C# and I am reading this code from an XML files and the layout of it is something like below...
<?xml version="1.0"?>
<accountlist>
<main>
<account id="1" special_id="4923959">
<username>Adam</username>
<motto>Hello Everyone>
<money>1004</money>
<friends>394</friends>
<rareid>9</rareid>
<mission>10</mission>
</account>
</main>
</accountlist>
How can I put each account tag into a string list? from the first < account > to the < / account > tag?
Please do NOT tell me to go to the link below as it does NOT work!!
How to read a XML file and write into List<>?
So far I have tried the below code, and the string list just stays empty
XDocument doc = XDocument.Parse(this._accountsFile);
List<string> list = doc.Root.Elements("account")
.Select(element => element.Value)
.ToList();
this._accounts = list;
You'll have to use Descendants instead of Elements:
List<string> list = doc.Root.Descendants("account").Descendants()
.Select(element => element.Value)
.ToList();
Elements only returns child elements of the element (in case of the root element this means <main>).
Descendants returns the entire tree inside the element.
Also: You'll have to fix the tag <motto>Hello Everyone> to <motto>Hello Everyone</motto>
This will work on your example (but you need to close this tag <motto>Hello Everyone>
public List<string> GetAccountsAsXmlList(string filePath)
{
XmlDocument x = new XmlDocument();
x.Load(filePath);
List<string> result = new List<string>();
XmlNode currentNode;
foreach (var accountNode in x.LastChild.FirstChild.ChildNodes)
{
currentNode = accountNode as XmlNode;
result.Add(currentNode.InnerXml);
}
return result;
}
EDIT as an answer to your question:
Is there a way I can get the id and specal_id in a seperate string?
you can use currentNode.Attributes["YourAttributeName"].Value, to get the values.
assume you have class Account :
class Account
{
public string accountXml { get; set; }
public string Id { get; set; }
public string Special_id { get; set; }
}
Then :
public List<Account> GetAccountsAsXmlList(string filePath)
{
XmlDocument x = new XmlDocument();
x.Load(filePath);
List<Account> result = new List<Account>();
XmlNode currentNode;
foreach (var accountNode in x.LastChild.FirstChild.ChildNodes)
{
currentNode = accountNode as XmlNode;
result.Add(new Account
{
accountXml = currentNode.InnerXml,
Id = currentNode.Attributes["id"].Value,
Special_id = currentNode.Attributes["special_id"].Value,
});
}
return result;
}
Use XPath to get the account element first:
using System.Xml.XPath;
XDocument doc = XDocument.Parse(xml);
foreach(var account in doc.XPathSelectElements("accountlist/main/account")){
List<string> list = account.Descendants()
.Select(element => element.Value)
.ToList();
}
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication37
{
class Program
{
static void Main(string[] args)
{
string input =
"<?xml version=\"1.0\"?>" +
"<accountlist>" +
"<main>" +
"<account id=\"1\" special_id=\"4923959\">" +
"<username>Adam</username>" +
"<motto>" +
"Hello Everyone>" +
"<money>1004</money>" +
"<friends>394</friends>" +
"<rareid>9</rareid>" +
"<mission>10</mission>" +
"</motto>" +
"</account>" +
"</main>" +
"</accountlist>";
XDocument doc = XDocument.Parse(input);
var results = doc.Descendants("accountlist").Select(x => new {
id = x.Element("main").Element("account").Attribute("id").Value,
special_id = x.Element("main").Element("account").Attribute("special_id").Value,
username = x.Element("main").Element("account").Element("username").Value,
motto = x.Element("main").Element("account").Element("motto").FirstNode.ToString(),
money = x.Element("main").Element("account").Element("motto").Element("money").Value,
friends = x.Element("main").Element("account").Element("motto").Element("friends").Value,
rareid = x.Element("main").Element("account").Element("motto").Element("rareid").Value,
mission = x.Element("main").Element("account").Element("motto").Element("mission").Value,
}).ToList();
}
}
}
Hi i am reading the RSS feed and creating an XML using the DataTable. This is my code
try
{
DataTable tbl = new DataTable();
tbl.Columns.Add("id");
tbl.Columns.Add("product_name");
tbl.Columns.Add("description");
//Extra Nodes
tbl.Columns.Add("brand");
tbl.Columns.Add("condition");
tbl.Columns.Add("product_type");
XmlDocument doc = new XmlDocument();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(s);
XmlNodeList itemNodes = xmlDoc.SelectNodes("//rss/channel/item");
foreach (XmlNode itemNode in itemNodes)
{
DataRow row = tbl.NewRow();
XmlNode idNode = itemNode.SelectSingleNode("id");
XmlNode product_nameNode = itemNode.SelectSingleNode("product_name");
XmlNode descriptionNode = itemNode.SelectSingleNode("description");
//extra nodes
XmlNode brandNode = itemNode.SelectSingleNode("brand");
XmlNode conditionNode = itemNode.SelectSingleNode("condition");
XmlNode product_typeNode = itemNode.SelectSingleNode("product_type");
if (idNode != null && product_nameNode != null && descriptionNode != null )
{
row[0] = idNode.InnerText;
row[1] = product_nameNode.InnerText;
row[2] = descriptionNode.InnerText;
//extra nodes
if (brandNode == null)
row[3] = "";
else
row[3] = brandNode.InnerText;
if (conditionNode==null)
row[4] = "";
else
row[4] = conditionNode.InnerText;
if (product_typeNode==null)
row[5] = "";
else
row[5] = product_typeNode.InnerText;
}
tbl.Rows.Add(row);
// tbl.Rows.Add(row);
}
}
}
catch (Exception ex)
{
// Console.WriteLine(ex.Message);
// Console.Read();
}
This is working fine without any issue but i want to make my code more efficient. Is this the good way to read the Rss and add into the datatable ? I am making a SSIS project on VS 2008 so i can not use SyndicationFeed .
You can use this code below ans use as example.
using System;
using System.ServiceModel.Syndication;
using System.Xml;
namespace RSSFeed
{
public class Program
{
static void Main(string[] args)
{
// URL from the site you need (RSS Feed in XML please).
String url = "http://www.medicalnewstoday.com/rss/abortion.xml";
// Create XML Reader.
using (XmlReader xmlReader = XmlReader.Create(url, new XmlReaderSettings() { DtdProcessing = DtdProcessing.Ignore }))
{
// Load The Feed.
SyndicationFeed syndicationFeed = SyndicationFeed.Load(xmlReader);
// through the list.
foreach (SyndicationItem item in syndicationFeed.Items)
{
// You can use a lot of information here todo what you need.
// TODO...
// Examples
String subject = item.Title.Text;
String summary = item.Summary.Text;
}
xmlReader.Close();
}
}
}
}
I am using this XML file:
<root>
<level1 name="A">
<level2 name="A1" />
<level2 name="A2" />
</level1>
<level1 name="B">
<level2 name="B1" />
<level2 name="B2" />
</level1>
<level1 name="C" />
</root>
Could someone give me a C# code using LINQ, the simplest way to print this result:
(Note the extra space if it is a level2 node)
A
A1
A2
B
B1
B2
C
Currently I have written this code:
XDocument xdoc = XDocument.Load("data.xml"));
var lv1s = from lv1 in xdoc.Descendants("level1")
select lv1.Attribute("name").Value;
foreach (var lv1 in lv1s)
{
result.AppendLine(lv1);
var lv2s = from lv2 in xdoc...???
}
Try this.
using System.Xml.Linq;
void Main()
{
StringBuilder result = new StringBuilder();
//Load xml
XDocument xdoc = XDocument.Load("data.xml");
//Run query
var lv1s = from lv1 in xdoc.Descendants("level1")
select new {
Header = lv1.Attribute("name").Value,
Children = lv1.Descendants("level2")
};
//Loop through results
foreach (var lv1 in lv1s){
result.AppendLine(lv1.Header);
foreach(var lv2 in lv1.Children)
result.AppendLine(" " + lv2.Attribute("name").Value);
}
Console.WriteLine(result);
}
Or, if you want a more general approach - i.e. for nesting up to "levelN":
void Main()
{
XElement rootElement = XElement.Load(#"c:\events\test.xml");
Console.WriteLine(GetOutline(0, rootElement));
}
private string GetOutline(int indentLevel, XElement element)
{
StringBuilder result = new StringBuilder();
if (element.Attribute("name") != null)
{
result = result.AppendLine(new string(' ', indentLevel * 2) + element.Attribute("name").Value);
}
foreach (XElement childElement in element.Elements())
{
result.Append(GetOutline(indentLevel + 1, childElement));
}
return result.ToString();
}
A couple of plain old foreach loops provides a clean solution:
foreach (XElement level1Element in XElement.Load("data.xml").Elements("level1"))
{
result.AppendLine(level1Element.Attribute("name").Value);
foreach (XElement level2Element in level1Element.Elements("level2"))
{
result.AppendLine(" " + level2Element.Attribute("name").Value);
}
}
Here are a couple of complete working examples that build on the #bendewey & #dommer examples. I needed to tweak each one a bit to get it to work, but in case another LINQ noob is looking for working examples, here you go:
//bendewey's example using data.xml from OP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
class loadXMLToLINQ1
{
static void Main( )
{
//Load xml
XDocument xdoc = XDocument.Load(#"c:\\data.xml"); //you'll have to edit your path
//Run query
var lv1s = from lv1 in xdoc.Descendants("level1")
select new
{
Header = lv1.Attribute("name").Value,
Children = lv1.Descendants("level2")
};
StringBuilder result = new StringBuilder(); //had to add this to make the result work
//Loop through results
foreach (var lv1 in lv1s)
{
result.AppendLine(" " + lv1.Header);
foreach(var lv2 in lv1.Children)
result.AppendLine(" " + lv2.Attribute("name").Value);
}
Console.WriteLine(result.ToString()); //added this so you could see the output on the console
}
}
And next:
//Dommer's example, using data.xml from OP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
class loadXMLToLINQ
{
static void Main( )
{
XElement rootElement = XElement.Load(#"c:\\data.xml"); //you'll have to edit your path
Console.WriteLine(GetOutline(0, rootElement));
}
static private string GetOutline(int indentLevel, XElement element)
{
StringBuilder result = new StringBuilder();
if (element.Attribute("name") != null)
{
result = result.AppendLine(new string(' ', indentLevel * 2) + element.Attribute("name").Value);
}
foreach (XElement childElement in element.Elements())
{
result.Append(GetOutline(indentLevel + 1, childElement));
}
return result.ToString();
}
}
These both compile & work in VS2010 using csc.exe version 4.0.30319.1 and give the exact same output. Hopefully these help someone else who's looking for working examples of code.
EDIT: added #eglasius' example as well since it became useful to me:
//#eglasius example, still using data.xml from OP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
class loadXMLToLINQ2
{
static void Main( )
{
StringBuilder result = new StringBuilder(); //needed for result below
XDocument xdoc = XDocument.Load(#"c:\\deg\\data.xml"); //you'll have to edit your path
var lv1s = xdoc.Root.Descendants("level1");
var lvs = lv1s.SelectMany(l=>
new string[]{ l.Attribute("name").Value }
.Union(
l.Descendants("level2")
.Select(l2=>" " + l2.Attribute("name").Value)
)
);
foreach (var lv in lvs)
{
result.AppendLine(lv);
}
Console.WriteLine(result);//added this so you could see the result
}
}
XDocument xdoc = XDocument.Load("data.xml");
var lv1s = xdoc.Root.Descendants("level1");
var lvs = lv1s.SelectMany(l=>
new string[]{ l.Attribute("name").Value }
.Union(
l.Descendants("level2")
.Select(l2=>" " + l2.Attribute("name").Value)
)
);
foreach (var lv in lvs)
{
result.AppendLine(lv);
}
Ps. You have to use .Root on any of these versions.
Asynchronous loading of the XML file can improve performance, especially if the file is large or if it takes a long time to load. In this example, we use the XDocument.LoadAsync method to load and parse the XML file asynchronously, which can help to prevent the application from becoming unresponsive while the file is being loaded.
Demo: https://dotnetfiddle.net/PGFE7c (using XML string parsing)
Implementation:
XDocument doc;
// Open the XML file using File.OpenRead and pass the stream to
// XDocument.LoadAsync to load and parse the XML asynchronously
using (var stream = File.OpenRead("data.xml"))
{
doc = await XDocument.LoadAsync(stream, LoadOptions.None, CancellationToken.None);
}
// Select the level1 elements from the document and create an anonymous object for each element
// with a Name property containing the value of the "name" attribute and a Children property
// containing a collection of the names of the level2 elements
var results = doc.Descendants("level1")
.Select(level1 => new
{
Name = level1.Attribute("name").Value,
Children = level1.Descendants("level2")
.Select(level2 => level2.Attribute("name").Value)
});
foreach (var result in results)
{
Console.WriteLine(result.Name);
foreach (var child in result.Children)
Console.WriteLine(" " + child);
}
Result:
A
A1
A2
B
B1
B2
C
I'm a LAMP guy and ended up working this small news module for an asp.net site, which I am having some difficulty with. I basically am adding and deleting elements via AJAX based on the id. Before, I had it working based on the the index of a set of elements, but would have issues deleting, since the index would change in the xml file and not on the page (since I am using ajax).
Here is the rundown
news.xml
<?xml version="1.0" encoding="utf-8"?>
<news>
<article id="1">
<title>Red Shield Environmental implements the PARCSuite system</title>
<story>Add stuff here</story>
</article>
<article id="2">
<title>Catalyst Paper selects PARCSuite for its Mill-Wide Process...</title>
<story>Add stuff here</story>
</article>
<article id="3">
<title>Weyerhaeuser uses Capstone Technology to provide Control...</title>
<story>Add stuff here</story>
</article>
</news>
Page sending del request:
<script type="text/javascript">
$(document).ready(function () {
$('.del').click(function () {
var obj = $(this);
var id = obj.attr('rel');
$.post('add-news-item.aspx',
{ id: id },
function () {
obj.parent().next().remove();
obj.parent().remove();
}
);
});
});
</script>
<a class="del" rel="1">...</a>
<a class="del" rel="1">...</a>
<a class="del" rel="1">...</a>
My functions
protected void addEntry(string title, string story)
{
XmlDocument news = new XmlDocument();
news.Load(Server.MapPath("../news.xml"));
XmlAttributeCollection ids = news.Attributes;
//Create a new node
XmlElement newelement = news.CreateElement("article");
XmlElement xmlTitle = news.CreateElement("title");
XmlElement xmlStory = news.CreateElement("story");
XmlAttribute id = ids[0];
int myId = int.Parse(id.Value + 1);
id.Value = ""+myId;
newelement.SetAttributeNode(id);
xmlTitle.InnerText = this.TitleBox.Text.Trim();
xmlStory.InnerText = this.StoryBox.Text.Trim();
newelement.AppendChild(xmlTitle);
newelement.AppendChild(xmlStory);
news.DocumentElement.AppendChild(newelement);
news.Save(Server.MapPath("../news.xml"));
}
protected void deleteEntry(int selectIndex)
{
XmlDocument news = new XmlDocument();
news.Load(Server.MapPath("../news.xml"));
XmlNode xmlnode = news.DocumentElement.ChildNodes.Item(selectIndex);
xmlnode.ParentNode.RemoveChild(xmlnode);
news.Save(Server.MapPath("../news.xml"));
}
I haven't updated deleteEntry() and you can see, I was using the array index but need to delete the article element based on the article id being passed. And when adding an entry, I need to set the id to the last elements id + 1. Yes, I know SQL would be 100 times easier, but I don't have access so... help?
Linq to XML should make this a lot simpler. Here would be the equivalent of what you are trying:
public void AddEntry(string title, string story)
{
var newElement = new XElement("article", new XElement("title", title), new XElement("story", story));
XDocument doc = XDocument.Parse(testXml);
var maxId = doc.Descendants("article").Attributes("id").Max(x => int.Parse(x.Value));
newElement.Add(new XAttribute("id", ++maxId));
doc.Descendants("news").First().Add(newElement);
//save the document
}
public void DeleteEntry(int selectIndex)
{
XDocument doc = XDocument.Parse(testXml);
doc.Descendants("article").Where(x => int.Parse(x.Attribute("id").Value) == selectIndex).Remove();
//save the document
}
Based on the size of your xml file and the number of requests, you may want to look into other approaches than loading the document, and saving it for each call to add and delete.
EDIT: Note that you would need to add null checks to the above code...
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace LinqToXML
{
class Program
{
static void Main(string[] args)
{
int ch;
do
{
Console.WriteLine("Enter the operation you want to execute");
Console.WriteLine("Press 1 to show previous record");
Console.WriteLine("Press 2 to add new record");
Console.WriteLine("Press 3 to delete record");
Console.WriteLine("Press 4 to update record");
Record RecordObject = new Record();
int UserChoice = Convert.ToInt32(Console.ReadLine());
switch (UserChoice)
{
case 1:
RecordObject.ShowRecord();
break;
case 2:
RecordObject.AddRecord();
break;
case 3:
RecordObject.DeleteRecord();
break;
case 4:
RecordObject.UpdateRecord();
break;
default:
Console.WriteLine("Invalid Option");
break;
}
Console.WriteLine("\tDo you Want to CONTINUE?\n\t1.YES\n\t2.NO");
ch = Convert.ToInt32(Console.ReadLine());
} while (ch == 1);
}
}
class Info
{
public string StudentName { get; set; }
public int StudentId { get; set; }
public int StudentAge { get; set; }
public string StudentCity { get; set; }
}
class Record
{
string fileAddress = #"C:\XML.xml";
XmlDocument doc = new XmlDocument();
public void ShowRecord()
{
if (File.Exists(fileAddress))
{
string line;
using (StreamReader sr = new StreamReader(fileAddress))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.ReadLine();
}
}
else
{
Console.WriteLine("No record exist");
}
}
public void AddRecord()
{
Console.WriteLine("Enter Student Name :");
string StuName = Console.ReadLine();
Console.WriteLine("Enter Student Age :");
int StuAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student City :");
string StuCity = Console.ReadLine();
Info InfoObj = new Info();
InfoObj.StudentName = StuName;
InfoObj.StudentAge = StuAge;
InfoObj.StudentCity = StuCity;
FileStream FileStreamObj = null;
if (File.Exists(fileAddress))
{
FileStreamObj = new FileStream(fileAddress, FileMode.Open, FileAccess.ReadWrite);
FileStreamObj.Close();
XmlDocument doc = new XmlDocument();
doc.Load(fileAddress);
XmlNodeList nodes = doc.SelectNodes("//students/student");
int nodeCount = nodes.Count;
nodeCount++;
XmlNodeList students = doc.SelectNodes("//students");
foreach (XmlNode student in students)
{
XmlNode parentNode = doc.CreateElement("student");
XmlAttribute attribute = doc.CreateAttribute("id");
attribute.Value = nodeCount.ToString();
parentNode.Attributes.Append(attribute);
student.AppendChild(parentNode);
XmlNode studentName = doc.CreateElement("studentName");
studentName.InnerText = StuName;
parentNode.AppendChild(studentName);
XmlNode studentAge = doc.CreateElement("studentAge");
studentAge.InnerText = StuAge.ToString();
parentNode.AppendChild(studentAge);
XmlNode studentCity = doc.CreateElement("studentCity");
studentCity.InnerText = StuCity;
parentNode.AppendChild(studentCity);
doc.Save(fileAddress);
}
}
else
{
FileStreamObj = new FileStream(fileAddress, FileMode.Create, FileAccess.ReadWrite);
FileStreamObj.Close();
int StudentId = 1;
XmlDocument doc = new XmlDocument();
XmlNode rootNode = doc.CreateElement("students");
doc.AppendChild(rootNode);
XmlNode parentNode = doc.CreateElement("student");
XmlAttribute attribute = doc.CreateAttribute("id");
attribute.Value = StudentId.ToString();
parentNode.Attributes.Append(attribute);
rootNode.AppendChild(parentNode);
XmlNode studentName = doc.CreateElement("studentName");
studentName.InnerText = StuName;
parentNode.AppendChild(studentName);
XmlNode studentAge = doc.CreateElement("studentAge");
studentAge.InnerText = StuAge.ToString();
parentNode.AppendChild(studentAge);
XmlNode studentCity = doc.CreateElement("studentCity");
studentCity.InnerText = StuCity;
parentNode.AppendChild(studentCity);
doc.Save(fileAddress);
}
}
public void UpdateRecord()
{
doc.Load(fileAddress);
Console.WriteLine("Enter ID of the record you want to update");
int InputChoice = Convert.ToInt32(Console.ReadLine());
Info infoObj = new Info();
XmlElement element = doc.DocumentElement;
XmlNode nodeElement = element.SelectSingleNode("student[#id='" + InputChoice + "']");
if (nodeElement == null)
{
Console.WriteLine("Record doesn't exist");
}
else
{
string oldName = nodeElement.ChildNodes[0].InnerText;
string oldAge = nodeElement.ChildNodes[1].InnerText;
string oldCity = nodeElement.ChildNodes[2].InnerText;
infoObj.StudentName = oldName;
infoObj.StudentAge = Convert.ToInt32(oldAge);
infoObj.StudentCity = oldCity;
Console.WriteLine("Old Values are:\n\tName: " + infoObj.StudentName + "\n\tAge" + infoObj.StudentAge + " \n\tCity" + infoObj.StudentCity + "");
Console.WriteLine("Enter new name");
string newName = Console.ReadLine();
Console.WriteLine("Enter new Age");
int newAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter new city");
string newCity = Console.ReadLine();
infoObj.StudentName = newName;
infoObj.StudentAge = newAge;
infoObj.StudentCity = newCity;
nodeElement.ChildNodes[0].InnerText = infoObj.StudentName;
nodeElement.ChildNodes[1].InnerText = infoObj.StudentAge.ToString();
nodeElement.ChildNodes[2].InnerText = infoObj.StudentCity;
doc.Save(fileAddress);
}
}
public void DeleteRecord()
{
doc.Load(fileAddress);
Console.WriteLine("Enter the Id you want to delete");
string inputValue = Console.ReadLine();
XmlElement element = doc.DocumentElement;
XmlNode nodeElement = element.SelectSingleNode("student[#id='" + inputValue + "']");
if (nodeElement == null)
{
Console.WriteLine("Record doesn't exist");
}
else
{
element.RemoveChild(nodeElement);
doc.Save(fileAddress);
Console.WriteLine("Sucessfully deleted");
}
}
}
}