How to add elements in an XML file? - c#

How should I add X there in XElement ?
XDocument triggerDocument = new XDocument(
new XDeclaration("1.0", "utf-8", null));
XElement triggerRoot = new XElement("config",
new XElement("maketool-config",
new XElement("hmi", new XElement("Messages",X))));
triggerDocument.Add(triggerRoot);
triggerDocument.Save(Path.Combine(outPath, "_triggers.xml"));
for (int i = 0; i <= events.Count; i++)
{
foreach (var item in events)
{
triggerRoot.Add(new XElement("n",
new XAttribute("page", item.page),
new XAttribute("sequence", item.sequence),
new XAttribute("priority", item.priority),
new XAttribute("errorText", item.errorText)
));
}
}
so it should look like this :
<?xml version="1.0" encoding="utf-8"?>
<config schema ="sdk-hmi.xsd">
<maketool-config>
<hmi>
<messages>
<n page="" sequence="" priority="" errorText="" />
<n page="" sequence="" priority="" errorText="" />
<n page="" sequence="" priority="" errorText="" />
<n page="" sequence="" priority="" errorText="" />
<n page="" sequence="" priority="" errorText="" />
</messages>
</hmi>
</maketool-config>
</config>

You can pass an XElement[] or IEnumerable<XElement> to XElement's constructor:
var messages = events.Select(item => new XElement("n",
new XAttribute("page", item.page),
new XAttribute("sequence", item.sequence),
new XAttribute("priority", item.priority),
new XAttribute("errorText", item.errorText)
));
XDocument triggerDocument = new XDocument(
new XDeclaration("1.0", "utf-8", null));
XElement triggerRoot = new XElement("config",
new XElement("maketool-config",
new XElement("hmi",
new XElement("Messages", messages))) // <<<--- This is the important part.
);
triggerDocument.Add(triggerRoot);

You can try this:
XDocument triggerDocument = new XDocument(
new XDeclaration("1.0", "utf-8", null));
XElement triggerRoot = new XElement("config",
new XElement("maketool-config",
new XElement("hmi", new XElement("Messages"))));
triggerDocument.Add(triggerRoot);
XElement msgNode = triggerRoot.Elements("Messages")
.SingleOrDefault();
if (msgNode != null)
{
foreach (var item in events)
{
msgNode.Add(new XElement("n",
new XAttribute("page", item.page),
new XAttribute("sequence", item.sequence),
new XAttribute("priority", item.priority),
new XAttribute("errorText", item.errorText)
));
}
}

May this will help to add nodes...
//file name
string filename = #"d:\temp\XMLFile2.xml";
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
//load from file
doc.Load(filename);
//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
node.InnerText = "this is new node";
//add to elements collection
doc.DocumentElement.AppendChild(node);
//save back
doc.Save(filename);

Related

XElement with LINQ add node after specific node

I've got this XML:
<?xml version="1.0" encoding="utf-8"?>
<JMF SenderID="InkZone-Controller" Version="1.2">
<Command ID="cmd.00695" Type="Resource">
<ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
<InkZoneProfile ID="r0013" Class="Parameter" Locked="false" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
<InkZoneProfile SignatureName="SIG1">
<InkZoneProfile Locked="False" SheetName="S1">
<InkZoneProfile Side="Front">
<ColorPool Class="Parameter" DescriptiveName="Colors for the job" Status="Available">
<InkZoneProfile Separation="PANTONE 647 C" ZoneSettingsX="0 0,003 " />
</ColorPool>
</InkZoneProfile>
</InkZoneProfile>
</InkZoneProfile>
</InkZoneProfile>
</ResourceCmdParams>
</Command>
</JMF>
I'm trying to add a node after a specific node() , using XElement and Linq. But my LINQ query always returns me null.
Tried this:
XElement InkZonePath = XmlDoc.Element("JMF").Elements("InkZoneProfile").Where(z => z.Element("InkZoneProfile").Attribute("Side").Value == "Front").SingleOrDefault();
And this:
XmlDoc.Element("JMF")
.Elements("InkZoneProfile").Where(InkZoneProfile => InkZoneProfile.Attribute("Side")
.Value == "Front").FirstOrDefault().AddAfterSelf(new XElement("InkZoneProfile",
new XAttribute("Separation", x.colorname),
new XAttribute("ZoneSettingsX", x.colorvalues)));
I've built this queries following those examples:
Select XElement where child element has a value
Insert XElements after a specific node
LINQ-to-XML XElement query NULL
But it didn't worked as expected. What is wrong with the LINQ Query ? From what i've read it should work (logically reading the expression i can understand it).
Thanks
EDIT-1: Entire writexml Method
public void writexml(xmldatalist XMLList, variables GlobalVars)
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t",
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace,
Encoding = new UTF8Encoding(false)
};
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string FileExtension = ".xml";
string PathString = Path.Combine(DesktopFolder, "XML");
System.IO.Directory.CreateDirectory(PathString);
foreach (List<xmldata> i in XMLList.XMLArrayList)
{
int m = 0;
foreach (var x in i)
{
string XMLFilename = System.IO.Path.GetFileNameWithoutExtension(x.xml_filename);
GlobalVars.FullPath = Path.Combine(PathString, XMLFilename + FileExtension);
if (!File.Exists(GlobalVars.FullPath))
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("JMF",
new XAttribute("SenderID", "InkZone-Controller"),
new XAttribute("Version", "1.2"),
new XElement("Command",
new XAttribute("ID", "cmd.00695"),
new XAttribute("Type", "Resource"),
new XElement("ResourceCmdParams",
new XAttribute("ResourceName", "InkZoneProfile"),
new XAttribute("JobID", "K_41"),
new XElement("InkZoneProfile",
new XAttribute("ID", "r0013"),
new XAttribute("Class", "Parameter"),
new XAttribute("Locked", "False"),
new XAttribute("Status", "Available"),
new XAttribute("PartIDKeys", "SignatureName SheetName Side Separation"),
new XAttribute("DescriptiveName", "Schieberwerte von DI"),
new XAttribute("ZoneWidth", "32"),
new XElement("InkZoneProfile",
new XAttribute("SignatureName", "SIG1"),
new XElement("InkZoneProfile",
new XAttribute("Locked", "false"),
new XAttribute("SheetName", "S1"),
new XElement("InkZoneProfile",
new XAttribute("Side", "Front"),
new XElement("ColorPoolClass",
new XAttribute("Class", "Parameter"),
new XAttribute("DescriptiveName", "Colors for the job"),
new XAttribute("Status", "Available")
)))))))));
doc.Save(GlobalVars.FullPath);
XDocument XmlDoc = new XDocument();
XmlDoc = XDocument.Load(GlobalVars.FullPath);
XElement InkZonePath = XmlDoc.Root.Descendants("InkZoneProfile").Where(z => (string)z.Attribute("Side") == "Front").SingleOrDefault();
if (InkZonePath != null)
{
InkZonePath.AddAfterSelf(new XElement("InkZoneProfile",
new XAttribute("Separation", x.colorname),
new XAttribute("ZoneSettingsX", x.colorvalues)));
}
XmlDoc.Save(GlobalVars.FullPath);
}//Closing !FileExists
}//Closing inner foreach
}//Closing outer foreach
}//Closing writexml method
The problem with your current code is here : Element("JMF").Elements("InkZoneProfile") Since InkZoneProfile is not a direct child of JMF it will not return anything. Use Descendants instead.
Check difference between Elements & Descendants.
This should give you correct result:-
XElement InkZonePath = xdoc.Element("JMF").Descendants("InkZoneProfile")
.SingleOrDefault(z => (string)z.Attribute("Side") == "Front")
After this you can add whatever node you want to add using AddAfterSelf. Also note I have used SingleOrDefault here, but you may get exception if you have multiple matching nodes with this, In that case consider using FirstOrDefault.
Update:
To add new node:-
if (InkZonePath != null)
{
InkZonePath.AddAfterSelf(new XElement("InkZoneProfile",
new XAttribute("Separation", "Test"),
new XAttribute("ZoneSettingsX", "Test2")));
}
//Save XDocument
xdoc.Save(#"C:\Foo.xml");
You need to use Descendants method instead Elements:
XElement InkZonePath = XmlDoc.Root.Descendants("InkZoneProfile").Where(z => (string)z.Attribute("Side") == "Front").SingleOrDefault();
if(InkZonePath !=null)
InkZonePath.AddAfterSelf(new XElement("InkZoneProfile",
new XAttribute("Separation", x.colorname),
new XAttribute("ZoneSettingsX", x.colorvalues)));
you can use Descendants instead.
var node = XmlDoc.Descendants("InkZoneProfile").Where(x=> x.Attribute("Side") !=null && x.Attribute("Side").Value == "Front").FirstorDefault();

Adding an XMLNode below a specific node

Here is my XML structure
<?xml version="1.0" encoding="utf-8"?>
<Projects xmlns="urn:projects-schema">
<Project>
<Name>Project1</Name>
<Images>
<Image Path="D:/abc.jpg"></Image>
</Images>
</Project>
</Projects>
I want to be able to Add a new Project Node though my code
And I want to be able to Create a new Image Node given a the Project Name
For the first task I have this so far:
try
{
var filename = Server.MapPath("~/App_Data/Projects.xml");
var doc = new XmlDocument();
if (System.IO.File.Exists(filename))
{
if (!ProjectExists(projectName))
{
doc.Load(filename);
var root = doc.DocumentElement;
var newElement = doc.CreateElement("Project");
root.AppendChild(newElement);
root = doc.DocumentElement;
newElement = doc.CreateElement("Name");
var textNode = doc.CreateTextNode(projectName);
root.LastChild.AppendChild(newElement);
root.LastChild.LastChild.AppendChild(textNode);
doc.Save(filename);
}
else
throw new ApplicationException("Project already exists");
doc = null;
}
}
catch (Exception ex)
{
throw ex;
}
But I am struggling for the second part. This is what I have so far:
if (System.IO.File.Exists(projectFilename))
{
doc.Load(projectFilename);
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("prj", "urn:projects-schema");
root = root.SelectSingleNode("descendant::prj:Project[prj:Name='" + projectName + "']", nsManager);
}
Any help will be appreciated. Thanks
XElement projects = XElement.Parse (
#"<Projects xmlns='urn:projects-schema'>
<Project>
<Name>Project1</Name>
<Images>
<Image Path='D:/abc.jpg'></Image>
</Images>
</Project>
</Projects>");
For Task 1, this can be done as follows:
projects.LastNode.AddAfterSelf(new XElement("Project",
new XElement("Name", "Project2"),
new XElement("Images",
new XElement("Image", "", new XAttribute("Path", "C:/abc.jpg"))
)));
Task 2:
projects.Descendants("Name")
.Where(x => x.Value == projectName).FirstOrDefault()
.AddAfterSelf(new XElement("Images",
new XElement("Image", "", new XAttribute("Path", "C:/def.jpg"))
)
);
EDIT: To check if an Images element exists and add a child Image element with an attribute to it
projects.Descendants("Project")
.Where(x => x.Elements("Images").Any())
.Descendants("Images").FirstOrDefault()
.Add(new XElement("Image", "", new XAttribute("Path", "D:/xyz.jpg")));

Writing Xml in Windows Phone Application

I have this code that work fine to create an xml document for my WPF application.
var doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
var parentNode = doc.CreateElement("manga");
doc.AppendChild(parentNode);
foreach (var mList in mangaList)
{
var itemNode = doc.CreateElement("item");
var itemAttribute = doc.CreateAttribute("value");
itemAttribute.Value = mList.Key;
itemNode.InnerText = mList.Value;
itemNode.Attributes.Append(itemAttribute);
parentNode.AppendChild(itemNode);
}
var writer = new XmlTextWriter(#"Data\mangalist.xml", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
Now I want to create similar application for Windows Phone 7.5 and i'm stuck in porting above code to be able to run in WP. After quick searching i found that XmlDocument is not available in Windows Phone and have to switch using XDocument. I am far from familiar with XDocument and hope somebody can help to me make my windows phone apps outputting the same xml.
Thanks
Solution :
After good hints from #Pradeep Kesharwani and #dav_i I managed to port those codes above to use XDocument and StreamWriter instead of XmlDocument and XmlTextWriter which are not available for WP:
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement("manga");
var mangaList = new Dictionary<string, string>();
mangaList.Add("conan", "conan");
mangaList.Add("naruto", "naruto");
foreach (var mList in mangaList)
{
var itemNode = new XElement("item");
var itemAttribute = new XAttribute("value", mList.Key);
itemNode.Value = mList.Value;
itemNode.Add(itemAttribute);
root.Add(itemNode);
}
doc.Add(root);
using (var writer = new StreamWriter(#"Data\mangalist2.xml"))
{
writer.Write(doc.ToString());
}
As I said in comments, XDocument is pretty straight forward -
new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XElement("root",
new XElement("something",
new XAttribute("attribute", "asdf"),
new XElement("value", 1234),
new XElement("value2", 4567)
),
new XElement("something",
new XAttribute("attribute", "asdf"),
new XElement("value", 1234),
new XElement("value2", 4567)
)
)
)
Gives the following
<root>
<something attribute="asdf">
<value>1234</value>
<value2>4567</value2>
</something>
<something attribute="asdf">
<value>1234</value>
<value2>4567</value2>
</something>
</root>
Hopefully this will help you!
To automatically populate in a loop, you could do something like this:
var somethings = new List<XElement>();
for (int i = 0; i < 3; i++)
somethings.Add(new XElement("something", new XAttribute("attribute", i + 1)));
var document = new XDocument(
new XElement("root",
somethings));
Which results in
<root>
<something attribute="1" />
<something attribute="2" />
<something attribute="3" />
</root>
This Create method could be used to create a xml doc in wp7
private void CreateXml()
{
string xmlStr = "<RootNode></RootNode>";
XDocument document = XDocument.Parse(xmlStr);
XElement ex = new XElement(new XElement("FirstNOde"));
XElement ex1 = new XElement(new XElement("second"));
ex1.Value = "fdfgf";
ex.Add(ex1);
document.Root.Add(new XElement("ChildNode", "World!"));
document.Root.Add(new XElement("ChildNode", "World!"));
document.Root.Add(ex);
string newXmlStr = document.ToString();
}
This will be the created xml
<RootNode>
<ChildNode>World!</ChildNode>
<ChildNode>World!</ChildNode>
<FirstNOde>
<second>fdfgf</second>
</FirstNOde>
</RootNode>
public void ReadXml()
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("Your xml file name", FileMode.Open);
using (XmlReader reader = XmlReader.Create(isoFileStream))
{
XDocument xml = XDocument.Load(reader);
XElement root1 = xml.Root;
}
}

How to add another top level node

I have my linq code formatted like:
<Deck>
<Treasure>
<card>
.....
</card>
......
</treasure>
<Door>
<card>
.....
</card>
......
</Door>
In the following code how do I add another Door that is the same "level" as treasure? Everything I have tried keeps adding it as the same level as card. Here is what I have:
public void SaveXml(string path)
{
XElement xml;
XElement root = new XElement("Treasure");
foreach (var item in TreasureCards)
{
xml = new XElement("Card",
new XAttribute("name", item.Name),
new XElement("Type", item.Type),
new XElement("Image",
new XAttribute("path", item.Image)),
new XElement("Usage", item.Usage),
new XElement("Quantity", item.Quantity),
new XElement("Sell", item.Sell)
);
root.Add(xml);
}
root.Add(new XElement("Door"));
foreach (var item in DoorCards)
{
xml = new XElement("Card",
new XAttribute("name", item.Name),
new XElement("Type", item.Type),
new XElement("Image",
new XAttribute("path", item.Image)),
new XElement("Usage", item.Usage),
new XElement("Quantity", item.Quantity));
root.Add(xml);
}
You need to create the Deck element first:
XElement deck = new XElement("Deck");
Then add both the treasure (which i've taken the liberty of renaming from root to treasure) and the door to it:
XElement treasure = new XElement("Treasure")
...
deck.Add(treasure)
...
XElement door = new XElement("Door")
...
deck.Add(door)

Linq to XML - update/alter the nodes of an XML Document

I've got 2 Questions:
1.
I've sarted working around with Linq to XML and i'm wondering if it is possible to change an XML document via Linq. I mean, is there someting like
XDocument xmlDoc = XDocument.Load("sample.xml");
update item in xmlDoc.Descendants("item")
where (int)item .Attribute("id") == id
...
2.
I already know how to create and add a new XMLElement by simply using
xmlDoc.Element("items").Add(new XElement(......);
but how can I remove a single entry?
XML sample data:
<items>
<item id="1" name="sample1" info="sample1 info" web="" />
<item id="2" name="sample2" info="sample2 info" web="" />
</itmes>
thank you for your answer. everything works fine.
just as completition to my questions the code below shows how to modify a single entry:
string xml = #"<data><record id='1' info='sample Info'/><record id='2' info='sample Info'/><record id='3' info='sample Info'/></data>";
StringReader sr = new StringReader(xml);
XDocument d = XDocument.Load(sr);
d.Descendants("record").Where(x => x.Attribute("id").Value == "2").Single().SetAttributeValue("info", "new sample info");
Is this what you have in mind?
using System;
using System.Linq;
using System.Xml.Linq;
static void Main(string[] args)
{
string xml = #"<data><record id='1'/><record id='2'/><record id='3'/></data>";
StringReader sr = new StringReader(xml);
XDocument d = XDocument.Load(sr);
// the verbose way, if you will be removing many elements (though in
// this case, we're only removing one)
var list = from XElement e in d.Descendants("record")
where e.Attribute("id").Value == "2"
select e;
// convert the list to an array so that we're not modifying the
// collection that we're iterating over
foreach (XElement e in list.ToArray())
{
e.Remove();
}
// the concise way, which only works if you're removing a single element
// (and will blow up if the element isn't found)
d.Descendants("record").Where(x => x.Attribute("id").Value == "3").Single().Remove();
XmlWriter xw = XmlWriter.Create(Console.Out);
d.WriteTo(xw);
xw.Flush();
Console.ReadLine();
}
The answers are in this thread...you just have to do a lot of sorting to find them, so I've done the work of compliling them for you:
YES you can edit elements
Deleting elements is easy: element.Remove(); (Remember to save the xDocument after that)
Now if you're reading this thread, you probably want to know HOW to edit elements. There are two different ways that data is stored in xml, e.g.:
<tagName attributeName="some value">another value</tagName>
As an ATTRIBUTE on a tag
As the content (read value) of the tag
To edit the value of an attribute, knox answered his own question:
d.Descendants("record").Where(x => x.Attribute("id").Value == "2").Single().SetAttributeValue("info", "new sample info");
In other words, get the XElement that you want to alter, and call element.SetAttributeValue("AttributeName", "new value for the attribute")
if you want to edit the value or contents of a tag, then Ajay answered it (if you dig through all his code):
persondata.Element("City").Value = txtCity.Text;
Or, in other words, once you have the XElement you're after, just use .Value and assign away.
Remember that after you perform any of these modifications on the elements in memory, you've got to call .Save() on the XDocument if you want to persist those changes to disk.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
//using System.Xml;
namespace XMLtoLinqApp
{
class Program
{
static void Main(string[] args)
{
//XmlDocument doc = new XmlDocument();
//XmlElement newBook=doc.CreateElement("BookParticipant");
//newBook.SetAttribute("Author");
//Using Functional Construction to Create an XML Schema
XElement xBookParticipant = new XElement("BookParticipant",
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"));
Console.WriteLine(xBookParticipant.ToString());
//Creates the Same XML Tree as Listing 6-1 but with Far Less Code
XElement xBookParticipants = new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham")));
Console.WriteLine(xBookParticipants.ToString());
//-- Disadvatages of XML document
//System.Xml.XmlElement xmlBookParticipant = new System.Xml.XmlElement("BookParticipant");
XElement xeBookParticipant = new XElement("BookParticipant");
XDocument xDocument = new XDocument(new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"))));
Console.WriteLine(xDocument.ToString());
//--Calling the ToString Method on an Element Produces the XML Tree
XElement name = new XElement("Name", "Joe");
Console.WriteLine(name.ToString());
//--Console.WriteLine Implicitly Calling the ToString Method on an Element to Produce an XML Tree
XElement name1 = new XElement("Person",
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"));
Console.WriteLine(name1);
//-- Casting an Element to Its Value’s Data Type Outputs the Value
Console.WriteLine(name);
Console.WriteLine((string)name);
//--Different Node Value Types Retrieved via Casting to the Node Value’s Type
XElement count = new XElement("Count", 12);
Console.WriteLine(count);
Console.WriteLine((int)count);
XElement smoker = new XElement("Smoker", false);
Console.WriteLine(smoker);
Console.WriteLine((bool)smoker);
XElement pi = new XElement("Pi", 3.1415926535);
Console.WriteLine(pi);
Console.WriteLine((double)pi);
DeferredQryProblem();
GenerateXMlFromLinqQry();
WithoutReaching();
Ancestors();
AncestorsAndSelf();
SortSample();
FindElementwithSpecificChild();
}
private static void DeferredQryProblem()
{
XDocument xDocument = new XDocument(
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
IEnumerable<XElement> elements =
xDocument.Element("BookParticipants").Elements("BookParticipant");
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
foreach (XElement element in elements)
{
Console.WriteLine("Removing {0} = {1} ...", element.Name, element.Value);
element.Remove();
}
Console.WriteLine(xDocument);
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
foreach (XElement element in elements.ToArray())
{
Console.WriteLine("Removing {0} = {1} ...", element.Name, element.Value);
element.Remove();
}
Console.WriteLine(xDocument);
}
//-- Creating an Attribute and Adding It to Its Element
private static void CreatingAttribute()
{
XElement xBookParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"));
Console.WriteLine(xBookParticipant);
}
//--Creating a Comment with Functional Construction
private static void CreatingComment()
{
XElement xBookParticipant = new XElement("BookParticipant",
new XComment("This person is retired."));
Console.WriteLine(xBookParticipant);
}
//--Creating a Declaration with Functional Construction
private static void CreateXmlDeclaration()
{
XDocument xDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("BookParticipant"));
Console.WriteLine(xDocument);
}
private static void GenerateXMlFromLinqQry()
{
BookParticipant[] bookParticipants = new[] {new BookParticipant {FirstName = "Joe", LastName = "Rattz",
ParticipantType = ParticipantTypes.Author},
new BookParticipant {FirstName = "Ewan", LastName = "Buckingham",
ParticipantType = ParticipantTypes.Editor}
};
XElement xBookParticipants =
new XElement("BookParticipants",
bookParticipants.Select(p =>
new XElement("BookParticipant",
new XAttribute("type", p.ParticipantType),
new XElement("FirstName", p.FirstName),
new XElement("LastName", p.LastName))));
Console.WriteLine(xBookParticipants);
}
//-- Obtaining Elements Without Reaching
private static void WithoutReaching()
{
XDocument xDocument = new XDocument(new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
//-- Simple Descendants
IEnumerable<XElement> elements = xDocument.Descendants("BookParticipant");
foreach (XElement element in elements)
{
Console.WriteLine("Element: {0} : value = {1}",
element.Name, element.Value);
}
//-- Descendants with Where Clause
IEnumerable<XElement> elements1 = xDocument.Descendants("BookParticipant")
.Where(e => ((string)e.Element("FirstName")) == "Ewan");
foreach (XElement element1 in elements1)
{
Console.WriteLine("Element: {0} : value = {1}",
element1.Name, element1.Value);
}
}
//-- Ancestors Prototype
private static void Ancestors()
{
XDocument xDocument = new XDocument(new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
IEnumerable<XElement> elements = xDocument.Element("BookParticipants").Descendants("FirstName");
// First, I will display the source elements.
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
// Now, I will display the ancestor elements for each source element.
foreach (XElement element in elements.Ancestors())
{
Console.WriteLine("Ancestor element: {0}", element.Name);
}
// Now, I will display the ancestor elements for each source element.
foreach (XElement element in elements.Ancestors("BookParticipant"))
{
Console.WriteLine("Ancestor element: {0}", element.Name);
}
}
//-- AncestorsAndSelf
private static void AncestorsAndSelf()
{
XDocument xDocument = new XDocument(
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
IEnumerable<XElement> elements =
xDocument.Element("BookParticipants").Descendants("FirstName");
// First, I will display the source elements.
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
// Now, I will display the ancestor elements for each source element.
foreach (XElement element in elements.AncestorsAndSelf())
{
Console.WriteLine("Ancestor element: {0}", element.Name);
}
// Now, I will display the ancestor elements for each source element.
foreach (XElement element in elements.AncestorsAndSelf("BookParticipant"))
{
Console.WriteLine("Ancestor element: {0}", element.Name);
}
}
//-- Sort Smaple
private static void SortSample()
{
XElement root = XElement.Load("Data.xml");
IEnumerable<decimal> prices =
from el in root.Elements("Data")
let price = (decimal)el.Element("Price")
orderby price
select price;
foreach (decimal el in prices)
Console.WriteLine(el);
}
//-- Find an Element with a Specific Child
private static void FindElementwithSpecificChild()
{
XElement root = XElement.Load("data.xml");
IEnumerable<XElement> tests =
from el in root.Elements("Data")
where (int)el.Element("Quantity") > 3
select el;
foreach (XElement el in tests)
Console.WriteLine((string)el.Attribute("TestId");
}
}
}
http://msdn.microsoft.com/en-us/library/bb387053.aspx
7.25-->
A
3
24.50
B
1
89.99
A
5
4.95
A
3
66.00
B
10
.99
A
15
29.00
B
8
6.99
static void Main(string[] args)
{
//XmlDocument doc = new XmlDocument();
//XmlElement newBook=doc.CreateElement("BookParticipant");
//newBook.SetAttribute("Author");
//Using Functional Construction to Create an XML Schema
XElement xBookParticipant = new XElement("BookParticipant",
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"));
Console.WriteLine(xBookParticipant.ToString());
//Creates the Same XML Tree as Listing 6-1 but with Far Less Code
XElement xBookParticipants = new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham")));
Console.WriteLine(xBookParticipants.ToString());
//-- Disadvatages of XML document
//System.Xml.XmlElement xmlBookParticipant = new System.Xml.XmlElement("BookParticipant");
XElement xeBookParticipant = new XElement("BookParticipant");
XDocument xDocument = new XDocument(new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"))));
Console.WriteLine(xDocument.ToString());
//--Calling the ToString Method on an Element Produces the XML Tree
XElement name = new XElement("Name", "Joe");
Console.WriteLine(name.ToString());
//--Console.WriteLine Implicitly Calling the ToString Method on an Element to Produce an XML Tree
XElement name1 = new XElement("Person",
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz"));
Console.WriteLine(name1);
//-- Casting an Element to Its Value’s Data Type Outputs the Value
Console.WriteLine(name);
Console.WriteLine((string)name);
//--Different Node Value Types Retrieved via Casting to the Node Value’s Type
XElement count = new XElement("Count", 12);
Console.WriteLine(count);
Console.WriteLine((int)count);
XElement smoker = new XElement("Smoker", false);
Console.WriteLine(smoker);
Console.WriteLine((bool)smoker);
XElement pi = new XElement("Pi", 3.1415926535);
Console.WriteLine(pi);
Console.WriteLine((double)pi);
DeferredQryProblem();
GenerateXMlFromLinqQry();
}
private static void DeferredQryProblem()
{
XDocument xDocument = new XDocument(
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
IEnumerable<XElement> elements =
xDocument.Element("BookParticipants").Elements("BookParticipant");
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
foreach (XElement element in elements)
{
Console.WriteLine("Removing {0} = {1} ...", element.Name, element.Value);
element.Remove();
}
Console.WriteLine(xDocument);
foreach (XElement element in elements)
{
Console.WriteLine("Source element: {0} : value = {1}",
element.Name, element.Value);
}
foreach (XElement element in elements.ToArray())
{
Console.WriteLine("Removing {0} = {1} ...", element.Name, element.Value);
element.Remove();
}
Console.WriteLine(xDocument);
}
//-- Creating an Attribute and Adding It to Its Element
private static void CreatingAttribute()
{
XElement xBookParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"));
Console.WriteLine(xBookParticipant);
}
//--Creating a Comment with Functional Construction
private static void CreatingComment()
{
XElement xBookParticipant = new XElement("BookParticipant",
new XComment("This person is retired."));
Console.WriteLine(xBookParticipant);
}
//--Creating a Declaration with Functional Construction
private static void CreateXmlDeclaration()
{
XDocument xDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("BookParticipant"));
Console.WriteLine(xDocument);
}
private static void GenerateXMlFromLinqQry()
{
BookParticipant[] bookParticipants = new[] {new BookParticipant {FirstName = "Joe", LastName = "Rattz",
ParticipantType = ParticipantTypes.Author},
new BookParticipant {FirstName = "Ewan", LastName = "Buckingham",
ParticipantType = ParticipantTypes.Editor}
};
XElement xBookParticipants =
new XElement("BookParticipants",
bookParticipants.Select(p =>
new XElement("BookParticipant",
new XAttribute("type", p.ParticipantType),
new XElement("FirstName", p.FirstName),
new XElement("LastName", p.LastName))));
Console.WriteLine(xBookParticipants);
}
//-- Obtaining Elements Without Reaching
private static void WithoutReaching()
{
XDocument xDocument = new XDocument(new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan"),
new XElement("LastName", "Buckingham"))));
IEnumerable<XElement> elements = xDocument.Descendants("BookParticipant");
foreach (XElement element in elements)
{
Console.WriteLine("Element: {0} : value = {1}",
element.Name, element.Value);
}
IEnumerable<XElement> elements1 = xDocument.Descendants("BookParticipant")
.Where(e => ((string)e.Element("FirstName")) == "Ewan");
foreach (XElement element1 in elements1)
{
Console.WriteLine("Element: {0} : value = {1}",
element1.Name, element1.Value);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace LinqToXmlTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ReadXml()
{
XDocument xdocument = XDocument.Load(Server.MapPath("People.xml"));
var persons = from person in xdocument.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value
};
litResults.Text = "";
foreach (var person in persons)
{
litResults.Text = litResults.Text + "Name: " + person.Name + "<br/>";
litResults.Text = litResults.Text + "City: " + person.City + "<br/>";
litResults.Text = litResults.Text + "Age: " + person.Age + "<br/><br/>";
}
if (litResults.Text == "")
litResults.Text = "No Results...";
}
protected void butAdd_Click(object sender, EventArgs e)
{
try
{
if (txtName.Text == "" || txtCity.Text == "" || txtAge.Text == "")
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Please Complete the form";
}
else
{
XDocument xdocumnet = XDocument.Load(Server.MapPath("People.xml"));
xdocumnet.Element("Persons").Add(new XElement("Person",
new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text),
new XElement("Age", txtAge.Text)));
xdocumnet.Save(Server.MapPath("People.xml"));
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "Data Successfully loaded to xml file";
txtName.Text = "";
txtCity.Text = "";
txtAge.Text = "";
ReadXml();
}
}
catch
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Sorry unable to precess request.Please try again";
}
}
protected void butRead_Click(object sender, EventArgs e)
{
ReadXml();
lblStatus.Text = "";
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (txtName.Text == "" || txtCity.Text == "" || txtAge.Text == "")
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Please enter all details in the form";
}
else
{
XDocument xdocument = XDocument.Load(Server.MapPath("People.xml"));
var persondata = (from person in xdocument.Descendants("Person")
where person.Element("Name").Value.Equals(txtName.Text)
select person).Single();
persondata.Element("City").Value = txtCity.Text;
persondata.Element("Age").Value = txtAge.Text;
xdocument.Save(Server.MapPath("People.xml"));
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "The data updated successfully";
ReadXml();
}
}
catch(Exception ex)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = ex.Message;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (txtName.Text == "")
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Please enter the name of the person to delete...";
}
else
{
XDocument xdocument = XDocument.Load(Server.MapPath("People.xml"));
var persondata = (from person in xdocument.Descendants("Person")
where person.Element("Name").Value.Equals(txtName.Text)
select person).Single();
persondata.Remove();
xdocument.Save(Server.MapPath("People.xml"));
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "The data deleted successfully...";
txtName.Text = "";
txtCity.Text = "";
txtAge.Text = "";
ReadXml();
}
}
catch (Exception ex)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = ex.Message;
}
}
}
}

Categories

Resources