Insert new XML node using LINQ - c#

XML:
<Questions>
<Question>
<Id>1</Id>
<Text>aaa</Text>
<Reserver />
</Question>
<Question>
<Id>2</Id>
<Text>bbb</Text>
<Reserver />
</Question>
</Questions>
How can insert new Question using LINQ like this:
<Question>
<Id>3</Id>
<Text>ccc</Text>
<Reserver />
</Question>

XDocument doc = XDocument.Parse("<Questions>...</Questions>");
doc.Root.Add(
new XElement("Question",
new XElement("Id", 3),
new XElement("Text", "ccc"),
new XElement("Reserver"))
);

You can create a new element like this:
var newElem = new XElement("Question",
new XElement("Id", 3),
...
);
xdoc.Root.Add(newElem);

Related

Insert data from C# to XML with using Linq issue?

I am building a C# application. I want to insert the following XML data to XML.
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee ID="1">
<Name>Numeri</Name>
</Employee>
<Employee ID="2">
<Name>Ismail</Name>
</Employee>
<Employee ID="3">
<Name>jemu</Name>
</Employee>
</Employees>
Previously I have tried an XML that has not attribute value, But now I
want to insert with attribute value.
string _file = (Application.StartupPath+"/employees.xml");
XDocument doc;
if (!File.Exists(_file))
{
doc = new XDocument();
doc.Add(new XElement("Employees"));
}
else
{
doc = XDocument.Load(_file);
}
doc.Root.Add(
new XElement("Employee",
new XElement("ID", textBox1.Text),
new XElement("Name", textBox2.Text)
)
);
doc.Save(_file);
You should use XAttribute instead of XElement in order to insert ID as attribute:
doc.Root.Add(
new XElement("Employee",
new XAttribute("ID", textBox1.Text),
new XElement("Name", textBox2.Text)
)
);

XML XElement building an xml document

Hi I am new to XML building, I basically didn't use it before, I always preferred json.
I have a solution where I just make it with string and convert to XML object, but how can I do it with XElement class?
This is the document:
<?xml version="1.0" encoding="utf-8"?>
<requestblock version="3.67">
<alias>ALIAS</alias>
<request type="AUTH">
<operation>
<sitereference>test12345</sitereference>
<accounttypedescription>TEST</accounttypedescription>
<parenttransactionreference>12-3-4567</parenttransactionreference>
</operation>
<merchant>
<orderreference>Example recurring auth</orderreference>
</merchant>
<customer> </customer>
<billing>
<amount currencycode="GBP">1234</amount>
<subscription type="TEST">
<number>1</number>
</subscription>
</billing>
<settlement/>
</request>
</requestblock>
I already have a part of the code like this:
XElement address =
new XElement("alias", "TEST",
new XElement("request", new XAttribute("type", "AUTH"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
));
But I have a problem with alias, because alias is closed after all elements, not in the same notation:
<alias>TEST
<request type="AUTH">
<City>Mercer Island</City>
<State>WA</State>
<Postal>68042</Postal>
</request>
</alias>
As you can see notation is the problem.
You're setting alias as your root element, which should be requestblock. If you start with requestblock like this:
XElement address =
new XElement("requestblock", new XAttribute("version",3.67),
new XElement("alias", "TEST"),
new XElement("request", new XAttribute("type", "AUTH"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
It'l give you
<requestblock version="3.67">
<alias>TEST</alias>
<request type="AUTH">
<City>Mercer Island</City>
<State>WA</State>
<Postal>68042</Postal>
</request>
</requestblock>

How to i loop the Xelement class and get the below result

Using a loop with xelement class in C# i would like to get the below result!
<data>
<description>Cities that I have recently visited.</description>
<cities>
<city id="1">
<name>Chicago1</name>
<state>IN1</state>
</city>
<city id="2">
<name>Chicago2</name>
<state>IN2</state>
</city>
<city id="3">
<name>Chicago3</name>
<state>IN3</state>
</city>
</cities>
</data>
This is the code i have tried so far! any help?? i need to use a loop and get the above values..The loop i used is commented..
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
XElement xmlDataStore = new XElement("data",
new XElement("cities",
new XElement("city", new XAttribute("id", "1")),
new XElement("city", "Colombo"),
new XElement("name", "lname"),
new XElement("state", "0772569984")
)
)
;
//var list = from x in XElement.ReadFrom(xmlDataStore).Element("Node").Elements()
//select new
//{
// Name = x.Name,
// Value = (string)x
//};
Console.WriteLine(xmlDataStore);
Console.ReadLine();
}
}
}
What i get...
<cities>
<city id="1">
<name>Chicago1</name>
<state>IN1</state>
</city>
</cities>
What i want...
<data>
<description>Cities that I have recently visited.</description>
<cities>
<city id="1">
<name>Chicago1</name>
<state>IN1</state>
</city>
<city id="2">
<name>Chicago2</name>
<state>IN2</state>
</city>
<city id="3">
<name>Chicago3</name>
<state>IN3</state>
</city>
</cities>
</data>
Okay, I still don't exactly know what the real problem is, so let's start with this:
private static void citiesXml()
{
const string desc = "Cities that I have recently visited.";
// set up a list of all the different cities
var list = new List<Tuple<string, string>>();
list.Add(new Tuple<string, string>("Chicago1", "IN1"));
list.Add(new Tuple<string, string>("Chicago2", "IN2"));
list.Add(new Tuple<string, string>("Chicago3", "IN3"));
var xmlDataStore = new XElement("data", new XElement("description", desc));
var xmlCities = new XElement("cities");
// loop through the list of cities and create a XElement for each single one
for (var i = 0; i < list.Count; i++)
{
xmlCities.Add(new XElement("city",
new XAttribute("id", i + 1),
new XElement("name", list[i].Item1),
new XElement("state", list[i].Item2)));
}
// add the cities to the data store object
xmlDataStore.Add(xmlCities);
Console.WriteLine(xmlDataStore);
Console.ReadLine();
}
This will print:
<data>
<description>Cities that I have recently visited.</description>
<cities>
<city id="1">
<name>Chicago1</name>
<state>IN1</state>
</city>
<city id="2">
<name>Chicago2</name>
<state>IN2</state>
</city>
<city id="3">
<name>Chicago3</name>
<state>IN3</state>
</city>
</cities>
</data>
So as far as I see it, the only difference is that there are no blank lines between cities. Are the missing blank lines the problem?
Check by Descendants
XDocument xdoc = XDocument.Load("Xml File Path"); //save that xml in "C:\test.xml "
IEnumerable<XElement> xEle = xdoc.XPathSelectElements("//cities");
if(xEle !=null)
{
foreach(XElement xE in Xelement.Descendants())
{
// here you will get everything ......
}
}
you can loop through

Xml simplification/extraction of distinct values - possible LINQ

Sorry for this long post....But i have a headache from this task.
I have a mile long xml document where I need to extract a list, use distinct values, and pass for transformation to web.
I have completed the task using xslt and keys, but the effort is forcing the server to its knees.
Description:
hundreds of products in xml, all with a number of named and Id'ed cattegories, all categories with at least one subcategory with name and id.
The categories are unique with ID, all subcategories are unique WITHIN that category:
Simplified example form the huge file (left our tons of info irrelevant to the task):
<?xml version="1.0" encoding="utf-8"?>
<root>
<productlist>
<product id="1">
<name>Some Product</name>
<categorylist>
<category id="1">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
<subcat id="2">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="2">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="3">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
</categorylist>
</product>
<product id="2">
<name>Some Product</name>
<categorylist>
<category id="1">
<name>cat1</name>
<subcategories>
<subcat id="2">
<name>subcat2</name>
</subcat>
<subcat id="4">
<name>subcat4</name>
</subcat>
</subcategories>
</category>
<category id="2">
<name>cat2</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="3">
<name>cat3</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
</categorylist>
</product>
</productlist>
</root>
DESIRED RESULT:
<?xml version="1.0" encoding="utf-8"?>
<root>
<maincat id="1">
<name>cat1</name>
<subcat id="1"><name>subcat1</name></subcat>
<subcat id="2"><name>subcat2</name></subcat>
<subcat id="3"><name>subcat3</name></subcat>
</maincat>
<maincat id="2">
<name>cat2</name>
<subcat id="1"><name>differentsubcat1</name></subcat>
<subcat id="2"><name>differentsubcat2</name></subcat>
<subcat id="3"><name>differentsubcat3</name></subcat>
</maincat>
<maincat id="2">
<name>cat2</name>
<subcat id="1"><name>differentsubcat1</name></subcat>
<subcat id="2"><name>differentsubcat2</name></subcat>
<subcat id="3"><name>differentsubcat3</name></subcat>
</maincat>
</root>
(original will from 2000 products produce 10 categories with from 5 to 15 subcategories)
Things tried:
Xslt with keys - works fine, but pooooor performance
Played around with linq:
IEnumerable<XElement> mainCats =
from Category1 in doc.Descendants("product").Descendants("category") select Category1;
var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
cDoc.Root.Add(mainCats);
cachedCategoryDoc = cDoc.ToString();
Result was a "categories only" (not distinct values of categories or subcategories)
Applied the same xlst to that, and got fairly better performance..... but still far from usable...
Can i apply some sort of magic with the linq statement to have the desired output??
A truckload of good karma goes out to the ones that can point me in det right direction..
//Steen
NOTE:
I am not stuck on using linq/XDocument if anyone has better options
Currently on .net 3.5, can switch to 4 if needed
If I understood your question corectly, here's a LINQ atempt.
The query below parses your XML data and creates a custom type which represents a category and contains the subcategories of that element.
After parsing, the data is grouped by category Id to get distinct subcategories for each category.
var doc = XElement.Load("path to the file");
var results = doc.Descendants("category")
.Select(cat => new
{
Id = cat.Attribute("id").Value,
Name = cat.Descendants("name").First().Value,
Subcategories = cat.Descendants("subcat")
.Select(subcat => new
{
Id = subcat.Attribute("id").Value,
Name = subcat.Descendants("name").First().Value
})
})
.GroupBy(x=>x.Id)
.Select(g=>new
{
Id = g.Key,
Name = g.First().Name,
Subcategories = g.SelectMany(x=>x.Subcategories).Distinct()
});
From the results above you can create your document using the code below:
var cdoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
cdoc.Root.Add(
results.Select(x=>
{
var element = new XElement("maincat", new XAttribute("id", x.Id));
element.Add(new XElement("name", x.Name));
element.Add(x.Subcategories.Select(c=>
{
var subcat = new XElement("subcat", new XAttribute("id", c.Id));
subcat.Add(new XElement("name", c.Name));
return subcat;
}).ToArray());
return element;
}));
Try this i have done something for it.. attributes are missing you can add them using XElement ctor
var doc = XDocument.Load(reader);
IEnumerable<XElement> mainCats =
doc.Descendants("product").Descendants("category").Select(r =>
new XElement("maincat", new XElement("name", r.Element("name").Value),
r.Descendants("subcat").Select(s => new XElement("subcat", new XElement("name", s.Element("name").Value)))));
var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
cDoc.Root.Add(mainCats);
var cachedCategoryDoc = cDoc.ToString();
Regards.
This will parse your xml into a dictionary of categories with all the distinct subcategory names. It uses XPath from this library: https://github.com/ChuckSavage/XmlLib/
XElement root = XElement.Load(file);
string[] cats = root.XGet("//category/name", string.Empty).Distinct().ToArray();
Dictionary<string, string[]> dict = new Dictionary<string, string[]>();
foreach (string cat in cats)
{
// Get all the categories by name and their subcat names
string[] subs = root
.XGet("//category[name={0}]/subcategories/subcat/name", string.Empty, cat)
.Distinct().ToArray();
dict.Add(cat, subs);
}
Or the parsing as one statement:
Dictionary<string, string[]> dict = root
.XGet("//category/name", string.Empty)
.Distinct()
.ToDictionary(cat => cat, cat => root
.XGet("//category[name={0}]/subcategories/subcat/name", string.Empty, cat)
.Distinct().ToArray());
I give you the task of assembling your resulting xml from the dictionary.

How do I do this in C#?

The following Unit Test passes in VB.Net
<Test()> _
Public Sub VB_XMLLiteral_SyntaxRocks_Test()
Dim XML = <Doc>
<Level1>
<Item id="1"/>
<Item id="2"/>
</Level1>
<Level1>
<Item id="3"/>
<Item id="4"/>
</Level1>
</Doc>
Assert.AreEqual(4, XML.<Level1>.<Item>.Count)
End Sub
How do I assert the same thing in C#?
To clarify, I 'd like to know how to express...
XML.<Level1>.<Item>
...in C#
Assert.AreEqual(4, XML.Elements("Level1").Elements("Item").Count());
And of course XML needs to be an XElement (that's what a VB literal produces too)
Using LINQ to XML:
var XML = new XElement("Doc",
new XElement("Level1",
new XElement("Item",
new XAttribute("Id", 1)),
new XElement("Item",
new XAttribute("Id", 2))),
new XElement("Level1",
new XElement("Item",
new XAttribute("Id", 3)),
new XElement("Item",
new XAttribute("Id", 4))));
Assert.AreEqual(4,
(from element in XML.Elements("Level1").Elements("Item")
select element).Count());

Categories