Adding child nodes to an XElement - c#

I am trying to append objects into an XML file. The problem I currently have is it appends everything at the first level itself. I am trying to have the list as the parent element and list items as the child elements.
What I've tried: I came across a few posts where they use loops but I am unable to relate that to my context and code.
Code:
XDocument xDocument = XDocument.Load(#"C:\Users\hci\Desktop\Nazish\TangramsTool\TangramsTool\patterndata.xml");
XElement root = xDocument.Element("Patterns");
foreach (Pattern currentPattern in PatternDictionary.Values)
{
String filePath = currentPattern.Name.ToString();
IEnumerable<XElement> rows = root.Descendants("Pattern"); // Returns a collection of the descendant elements for this document or element, in document order.
XElement firstRow = rows.First(); // Returns the first element of a sequence.
if (currentPattern.PatternDistancesList.Count() == 9)
{
firstRow.AddBeforeSelf( //Adds the specified content immediately before this node.
new XElement("Pattern"),
new XElement("Name", filePath.Substring(64)),
new XElement("PatternDistancesList"),
new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()),
}
}
Current XML File:
<Pattern/>
<Name>match.jpg</Name>
<PatternDistancesList/>
<PatternDistance>278</PatternDistance>
<PatternDistance>380</PatternDistance>
What I would like as the end result:
<Pattern>
<Name>match.jpg</Name>
<PatternDistancesList>
<PatternDistance>278</PatternDistance>
<PatternDistance>380</PatternDistance>
</PatternDistancesList>
<Pattern/>
Any tips will be much appreciated. I'm new to WPF and C# so still trying to learn things.

This should do the trick:
firstRow.AddBeforeSelf(
new XElement("Pattern",
new XElement("Name", filePath.Substring(64)),
new XElement("PatternDistancesList",
new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()))));

Related

DataGridView to XDocument Exception Error using foreach loop

I have the following code which correctly creates an xml document with the first record. But only because I have included a break after I add the first record (line)
xdoc.Add(line);
break;
if I let the loop run (in order to add all the records) I get and Exception
Error:{"This operation would create an incorrectly structured document."}
Had a Search on here and found lots of examples but can't quite get me head around them and how they relate to what I am trying to do.
XDocument xdoc = new XDocument();
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
XElement line = new XElement("line");
XElement itemid = new XElement("field", dr.Cells["Item ID"].Value.ToString());
itemid.Add(new XAttribute("name", "item_id"));
line.Add(itemid);
XElement itemname = new XElement("field", dr.Cells["Item Name"].Value.ToString());
itemname.Add(new XAttribute("name", "item_name"));
line.Add(itemname);
XElement cost = new XElement("field", dr.Cells["Cost"].Value.ToString());
cost.Add(new XAttribute("name", "cost"));
line.Add(cost);
xdoc.Add(line);
break;
}
xdoc.Save(#"C:\xmltest\test3.xml");
It adds the first line when i break out why not the rest when I remove the break?
Well any XML document needs some root element so change XDocument xdoc = new XDocument(); to e.g. XDocument xdoc = new XDocument(new XElement("root")); and then add to the root, that is, change xdoc.Add(line); to xdoc.Root.Add(line);.

How to generate an XML file dynamically using XDocument?

As I wrote in the subject itself , how can I do that?
Note that solution like this are not appropriate as I want to create child nodes dynamically through running..
new XDocument(
new XElement("root",
new XElement("someNode", "someValue")
)
)
.Save("foo.xml");
I guess this was clear enough the first time but I will write it again:
I need to be able to add child nodes to given parent node while running, in the current syntax I've written this is static generated xml which doesn't contribute me at all because all is known in advance, which is not as my case.
How would you do it with Xdocument, is there away?
If a document has a defined structure and should be filled with dynamic data, you can go like this:
// Setup base structure:
var doc = new XDocument(root);
var root = new XElement("items");
doc.Add(root);
// Retrieve some runtime data:
var data = new[] { 1, 2, 3, 4, 5 };
// Generate the rest of the document based on runtime data:
root.Add(data.Select(x => new XElement("item", x)));
Very simple
Please update your code accordingly
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("children");
xml.AppendChild(root);
XmlComment comment = xml.CreateComment("Children below...");
root.AppendChild(comment);
for(int i = 1; i < 10; i++)
{
XmlElement child = xml.CreateElement("child");
child.SetAttribute("age", i.ToString());
root.AppendChild(child);
}
string s = xml.OuterXml;

How can I get a node from an XML document based on its attribute value and into an XElement object?

I need to inject some XML into a pre-existing XML file under a certain node. Here is the code I have to create my XML:
//Define the nodes
XElement dataItemNode = new XElement("DataItem");
XElement setterNodeDisplayName = new XElement("Setter");
XElement setterNodeOU = new XElement("Setter");
//Create the tree with the nodes
dataItemNode.Add(setterNodeDisplayName);
dataItemNode.Add(setterNodeOU);
//Define the attributes
XAttribute nameAttrib = new XAttribute("Name", "OrganizationalUnits");
XAttribute displayNameAttrib = new XAttribute("Property", "DisplayName");
XAttribute ouAttrib = new XAttribute("Property", "OU");
//Attach the attributes to the nodes
setterNodeDisplayName.Add(displayNameAttrib);
setterNodeOU.Add(ouAttrib);
//Set the values for each node
setterNodeDisplayName.SetValue("TESTING DISPLAY NAME");
setterNodeOU.SetValue("OU=funky-butt,OU=super,OU=duper,OU=TMI,DC=rompa-room,DC=pbs,DC=com");
Here is the code I have so far to load up the XML document and try to get the node that I need to insert my XML under:
//Load up the UDI Wizard XML file
XDocument udiXML = XDocument.Load("UDIWizard_Config.xml");
//Get the node that I need to append to and then append my XML to it
XElement ouNode = THIS IS WHAT I DONT KNOW HOW TO DO
ouNode.Add(dataItemNode);
Here is the XML from the existing document I am trying to work with:
<Data Name="OrganizationalUnits">
<DataItem>
<Setter Property="DisplayName">TESTING DISPLAY NAME</Setter>
<Setter Property="OU">OU=funky-butt,OU=super,OU=duper,OU=TMI,DC=rompa-room,DC=pbs,DC=com</Setter>
</DataItem>
I have multiple nodes that with the name of "Data", but I need to get the node that is , and I don't know how. Just learning how to use XML with C#.
Thank you.
This will get the first Data node with Name attribute matching OrganizationalUnits:
var ouNode = udiXML
.Descendants("Data")
.Where(n => n.Attribute("Name") != null)
.Where(n => n.Attribute("Name").Value == "OrganizationalUnits")
.First();
If your document might contain Data nodes without Name attribute, extra check for null might be necessary.
Note that you can achieve the same result with XPath (this will select root Data node, you can get DataItem node using Element method):
var ouNode = udiXML.XPathSelectElement("//Data[#Name = 'OrganizationalUnits']");

C# - Unable to save a customized linq-to-xml file, with new elements in it?

I'm creating a new XDocument and inserting a root element "profiles" in it, then saving.
if (!System.IO.File.Exists("profiles.xml"))
{
XDocument doc = new XDocument(
new XElement("profiles")
);
doc.Save("profiles.xml", SaveOptions.None);
}
And then later I wanna take users input and add profiles into the already created xml file:
XElement profile =
new XElement(Player.Name,
new XElement("level", Player.Level),
new XElement("cash", Player.Cash)
);
XDocument doc = XDocument.Load("profiles.xml");
List<XElement> profiles = doc.Root.Elements().ToList();
for (int i = 0; i < profiles.Count; i++)
{
if (profiles[i].Name.ToString() == Player.name)
{
profiles[i] = profile;
return;
}
}
profile.Add(profile);
doc.Save("profiles.xml", SaveOptions.None);
But for some reason, it will never add any new profiles?
EDIT: Also, if I manually create a new profile into the xml file, it won't customize either, so the problem is within Saving the file?
You're never actually doing anything to change any of the elements within the XDocument that doc refers to:
If you find an element with the existing name, you're modifying the list, but that won't modify the document. You probably want to use XElement.ReplaceWith:
profiles[i].ReplaceWith(profile);
Note that in this case you're not even trying to save the XML file again (due to the return statement), so it's not really clear what you're trying to achieve in this case.
If you don't find the element, you're adding the profile element to itself, which certainly isn't going to modify the document. I suspect you want:
doc.Root.Add(profile);
In other words, add the new profile element as a new final child of the root element.
EDIT: Here's a different approach to try instead - I'm assuming any one name should only occur once:
XDocument doc = XDocument.Load("profiles.xml");
var existingElement = doc.Root
.Elements()
.Where(x => x.Name.ToString() == Player.name)
.FirstOrDefault();
if (existingElement != null)
{
existingElement.ReplaceWith(profile);
}
else
{
doc.Root.Add(profile);
}
doc.Save("profiles.xml", SaveOptions.None);
Also, I would strongly advise you not to use the player name as the element name. Use it as an attribute value or text value instead, e.g.
XElement profile =
new XElement("player",
new XAttribute("name", Player.Name),
new Attribute("level", Player.Level),
new XAttribute("cash", Player.Cash)
);
That way you won't have problems if the player name has spaces etc. You'd then need to change your query to:
var existingElement = doc.Root
.Elements()
.Where(x => (string) x.Attribute("name)" == Player.name)
.FirstOrDefault();

how to create N Level xml file

I need to create xml file that have N Level.
for ex. in my below example I have 'AlbumDetails' is root element and 'PrintPackage' is another child root and 'UpgradePackage' is another child root.
Can any one let me know how can i make N Level/Multi Level XML in c#.
<AlbumDetails>
<Album Id="203">
<Institute>Oxford</Institute>
<Venue>Wallingford School</Venue>
<PrintPackage>
<SizeName>Combination Pack</SizeName>
<Price>1.00</Price>
<Weight>60.00</Weight>
<UpgradePackage>
<SizeName>Upgrade 1</SizeName>
<Price>1.00</Price>
<Weight>60.00</Weight>
</UpgradePackage>
<SizeName>Standard Pack</SizeName>
<Price>90.0000</Price>
<Weight>600.0000</Weight>
</PrintPackage>
</Album>
</AlbumDetails>
You are looking for the XmlWriter class.
Update: In case you want to create a document similar to the one above:
var builder = new StringBuilder();
using (var writer = XmlWriter.Create(builder))
{
writer.WriteStartElement("AlbumDetails");
writer.WriteStartElement("Album");
writer.WriteAttributeString("Id", "203");
writer.WriteElementString("Venue", "Wallingford School");
writer.WriteStartElement("PrintPackage");
.... etc.
writer.WriteEndElement(); // close PrintPackage
writer.WriteEndElement(); // close Album
writer.WriteEndElement(); // close AlbumDetails
}
Console.WriteLine(builder.ToString());
Use XDocument and XElement from System.Xml.Linq ( Linq2Xml )
XDocument doc = new XDocument(new XDeclaration("1.0","utf-8","true"),
new XElement("AlbumDetails",
new XElement("Album",new XAttribute("Id","203"),
new XElement("Institute","Oxford"),
new XElement("Venue","Wallingford School")
...
)
)
);
If you are just looking for XElement only, you can build it up in a similar way. You can have a processingElement and create the XElement based on your logic and do
doc.Add(processingElement);
or
ele.Add(processingElement);
They're not really "child roots" - they're just elements which have other child elements.
Personally I'd use LINQ to XML. It's by far the simplest XML API I've used. For example:
var element = new XElement("AlbumDetails",
new XElement("Album",
new XAttribute("ID", 203"),
new XElement("Institute", "Oxford"),
new XElement("Venue", "Wallingford School"),
new XElement("PrintPackage",
new XElement("SizeName", "Combination Pack"),
// etc
new XElement("UpgradePackage",
new XElement("SizeName", "Upgrade 1"),
// etc
)
)
);
Of course, you don't have to build up everything in a single statement - you can add child nodes separately, potentially constructing them entirely separately. Indeed, you may want a separate method to create each "container" element.

Categories

Resources