LINQ to XML DOM (making a modifed Tree easily) - c#

I'd like to delete the last element and put somthing else instead of it.
I made some imaginary codes (** IT IS NOT Working one**).
I want to re-use upper part of it, because they are too long and changing all the time.
How can I make real ones ?
Thanks !!
var orignalTREE = new XDocument(
new XElement( "Root", new XAttribute("Id", "0"),
new XElement( "One", new XAttribute("Id", "1"),
new XElement( "Two", new XAttribute("Id", "2"),
new XElement( "Three", new XAttribute("Id", "3"),
new XElement( "Four", new XAttribute("Id", "4"),
new XElement( "Five", new XAttribute("Id", "5"),
new XElement("Six", new XAttribute("Id", "6"),
new XElement("Seven", new XAttribute("Id", "7"),
new XElement( "LAST", "It will be removed")
)))))))));
var extractedELMNT = orignalTREE.ElementsBeforeSelf("LAST");
orignalTREE.Descendants("LAST").Remove();
var modifiedTREE = new XDocument(
new XElement(
extractedELMNT.Select(x =>x),
new XElement( "Modified", new XAttribute("Testing...")
)));
MessageBox.Show( "Removed\n\n" + orignalTREE.ToString() +"\n\n\n"+
"Recovered\n\n" + modifiedTREE.ToString());
[Add]
I'd like to change the result from This one;
<Root Id="0">
<One Id="1">
<Two Id="2">
<Three Id="3">
<Four Id="4">
<Five Id="5">
<Six Id="6">
<Seven Id="7">
<LAST>It will be removed</LAST>
</Seven>
</Six>
</Five>
</Four>
</Three>
</Two>
</One>
</Root>
to This one.
<Root Id="0">
<One Id="1">
<Two Id="2">
<Three Id="3">
<Four Id="4">
<Five Id="5">
<Six Id="6">
<Seven Id="7">
<Modified>Testing..</Modified>
</Seven>
</Six>
</Five>
</Four>
</Three>
</Two>
</One>
</Root>
In general ways.

try this:
Updated:
var orignalTREE = new XDocument(
new XElement("Root", new XAttribute("Id", "0"),
new XElement("One", new XAttribute("Id", "1"),
new XElement("Two", new XAttribute("Id", "2"),
new XElement("Three", new XAttribute("Id", "3"),
new XElement("Four", new XAttribute("Id", "4"),
new XElement("Five", new XAttribute("Id", "5"),
new XElement("Six", new XAttribute("Id", "6"),
new XElement("Seven", new XAttribute("Id", "7"),
new XElement("LAST", "It will be removed")
)))))))));
var modifiedTREE = new XDocument(orignalTREE);
var parent = modifiedTREE.Descendants("LAST").FirstOrDefault().Parent;
modifiedTREE.Descendants("LAST").Remove();
parent.Add(new XElement("Modified", "Its attribute"));
Console.WriteLine("Removed\n\n" + orignalTREE.ToString() + "\n\n\n" +
"Recovered\n\n" + modifiedTREE.ToString());

Related

Assistance with how to properly create an xml file in c#

I'm working in c#, and I was needing to convert a .csv to xml. I pretty much got it down, and understand how to do it. However I'm struggling to have it right with how i need it setup.
XNamespace xNamespace = "urlhere";
XElement newXML = new XElement(xNamespace + "WarehouseReceipts",
new XAttribute("xmlns", "urlhere"),
from str in csv
let fields = str.Split(',')
select new XElement("WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", fields[9]),
new XElement("ShipperName", fields[10]),
new XElement("ConsigneeName", fields[11]),
new XElement("Items",
new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[0]),
new XElement("Pieces", fields[3]),
new XElement("Description", fields[2]),
new XElement("PackageName", fields[1]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[4]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[8]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[6]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[7]),
new XElement("WarehouseReceiptNumber", fields[9])
)
),
new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
)
)
);
return newXML;
Now, I will show you what the prints out in the console, so you can have an idea of how it comes out in the xml format.
<WarehouseReceipts xmlns="urlhere">
<WarehouseReceipt Type="WH" xmlns="">
<Number>"3519"</Number>
<ShipperName>"4 NET NETWORKING CORP"</ShipperName>
<ConsigneeName>"ACUAMAR"</ConsigneeName>
<Items>
<Item Type="WI">
<Satus>"On Hand"</Satus>
<Pieces>"10"</Pieces>
<Description>"APPLE NEW IPAD"</Description>
<PackageName>"Case"</PackageName>
<Length Unit="in">"5.00"</Length>
<Volume Unit="ft3">"0.60"</Volume>
<Height Unit="in">"0.60"</Height>
<Width Unit="in">"4.00"</Width>
<Weight Unit="lb">"10.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
<WarehouseReceipt Type="WH" xmlns="">
<Number>"3519"</Number>
<ShipperName>"4 NET NETWORKING CORP"</ShipperName>
<ConsigneeName>"ACUAMAR"</ConsigneeName>
<Items>
<Item Type="WI">
<Satus>"On Hand"</Satus>
<Pieces>"20"</Pieces>
<Description>"APPLE IMAC "</Description>
<PackageName>"Box"</PackageName>
<Length Unit="in">"35.00"</Length>
<Volume Unit="ft3">"273.40"</Volume>
<Height Unit="in">"273.40"</Height>
<Width Unit="in">"15.00"</Width>
<Weight Unit="lb">"400.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
</WarehouseReceipts>
That whats above, is incorrect, and i need it to come out how it is shown below.
<WarehouseReceipts xmlns="urlhere">
<WarehouseReceipt Type="WH">
<Number>WR-1-22</Number>
<ShipperName>shipper</ShipperName>
<ConsigneeName>consignee</ConsigneeName>
<Items>
<Item Type="WI">
<Status>OnHand</Status>
<Pieces>3</Pieces>
<Description>description2</Description>
<PackageName>Package type2</PackageName>
<WHRItemID>2</WHRItemID>
<Length Unit="in">4.00</Length>
<Height Unit="in">4.00</Height>
<Width Unit="in">4.00</Width>
<Weight Unit="lb">6.00000000000000088818</Weight>
<Volume Unit="ft3">0.11000000000000000056</Volume>
<Model>model2</Model>
<WarehouseReceiptNumber>WR-1-22</WarehouseReceiptNumber>
</Item>
<Item Type="WI">
<Status>OnHand</Status>
<Pieces>4</Pieces>
<Description>description</Description>
<PackageName>Package type</PackageName>
<LocationCode>RCV01</LocationCode>
<Length Unit="in">1.00</Length>
<Height Unit="in">3.00</Height>
<Width Unit="in">2.00</Width>
<Weight Unit="lb">16.00</Weight>
<Volume Unit="ft3">0.01000000000000000021</Volume>
<Model>model</Model>
<WarehouseReceiptNumber>WR-1-22</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
</WarehouseReceipts>
Any assistance on where i went wrong, would be greatly appreciated.
I used beyond compare to find difference. Not sure what you need help with. See picture
You are doing too much in one statement. See following :
XNamespace xNamespace = "urlhere";
string[] csv = null;
XElement WarehouseReceipt = null;
XElement items = null;
for(int i = 0; i < csv.Length; i++)
{
string[] fields = csv[i].Split(new char[] {','});
if(i == 0)
{
XElement newXML = new XElement(xNamespace + "WarehouseReceipts",
new XAttribute("xmlns", "urlhere"),
new XElement("WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", fields[9]),
new XElement("ShipperName", fields[10]),
new XElement("ConsigneeName", fields[11])
));
WarehouseReceipt = newXML.Descendants("WarehouseReceipt").FirstOrDefault();
items = new XElement("Items");
WarehouseReceipt.Add(items);
}
items.Add(new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[0]),
new XElement("Pieces", fields[3]),
new XElement("Description", fields[2]),
new XElement("PackageName", fields[1]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[4]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[8]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[6]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[7]),
new XElement("WarehouseReceiptNumber", fields[9])
));
WarehouseReceipt.Add(new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
));
}

Conditionally adding a XElement to a new XElement using c~

I am trying to use XElement to add new content to an existing XML data file.
For the most part it is working OK but I need to change it. This is what I have so far:
xdoc.Root.Add(
new XElement("W" + record.Date.ToString("yyyyMMdd"),
new XElement("Chairman", historyItem.Chairman),
new XElement("AuxCounsellor1", historyItem.AuxCounsellor1),
new XElement("AuxCounsellor2", historyItem.AuxCounsellor2),
new XElement("VideoConferenceHost", string.Empty),
new XElement("VideoConferenceCohost", string.Empty),
new XElement("PrayerOpen", historyItem.PrayerOpen),
new XElement("PrayerClose", historyItem.PrayerClose),
new XElement("CBSConductor", historyItem.CBSConductor),
new XElement("CBSReader", historyItem.CBSReader),
new XElement("ReviewQuestion", string.Empty),
new XElement("Items",
new XAttribute("ItemCount", historyItem.TalkItems.Count),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[0].Name),
new XElement("Theme", historyItem.TalkItems[0].Theme),
new XElement("Method", historyItem.TalkItems[0].Method)),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[1].Name),
new XElement("Theme", historyItem.TalkItems[1].Theme),
new XElement("Method", historyItem.TalkItems[1].Method)),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[2].Name),
new XElement("Theme", historyItem.TalkItems[2].Theme),
new XElement("Method", historyItem.TalkItems[2].Method)),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[3].Name),
new XElement("Theme", historyItem.TalkItems[3].Theme),
new XElement("Method", historyItem.TalkItems[3].Method)),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[4].Name),
new XElement("Theme", historyItem.TalkItems[4].Theme),
new XElement("Method", historyItem.TalkItems[4].Method)),
new XElement("Item",
new XElement("Name", historyItem.TalkItems[5].Name),
new XElement("Theme", historyItem.TalkItems[5].Theme),
new XElement("Method", historyItem.TalkItems[5].Method)))));
It creates an entry like this:
<W20220404>
<Chairman></Chairman>
<AuxCounsellor1></AuxCounsellor1>
<AuxCounsellor2></AuxCounsellor2>
<VideoConferenceHost></VideoConferenceHost>
<VideoConferenceCohost></VideoConferenceCohost>
<PrayerOpen></PrayerOpen>
<PrayerClose></PrayerClose>
<CBSConductor></CBSConductor>
<CBSReader></CBSReader>
<ReviewQuestion></ReviewQuestion>
<Items ItemCount="6">
<Item>
<Name></Name>
<Theme>“Come essere un vero amico”</Theme>
<Method>Talk</Method>
</Item>
<Item>
<Name></Name>
<Theme>Spiritual Gems</Theme>
<Method>Question and Answers</Method>
</Item>
<Item>
<Name></Name>
<Theme>Prepare This Month's Presentations</Theme>
<Method>Discussion with Video</Method>
</Item>
<Item>
<Name></Name>
<Theme>“Chi sono i tuoi amici online?”</Theme>
<Method>Discussion with Video</Method>
</Item>
<Item>
<Name></Name>
<Theme>Diamo il benvenuto agli ospiti</Theme>
<Method>Discussion with Video</Method>
</Item>
<Item>
<Name></Name>
<Theme></Theme>
<Method>Talk</Method>
</Item>
</Items>
</W20220404>
Now, I want to add another item into this W20220404 node, just after the list of Items. Eg:
<Teaching>
<Name Class="0">Brother 9</Name>
<Name Class="1">Brother 1</Name>
<Name Class="2">Brother 3</Name>
</Teaching>
But:
I only want to add the first Name if historyItem.Classes >= 1.
I only want to add the second Name if historyItem.Classes >= 2.
I only want to add the third Name if historyItem.Classes == 3.
I can't work out how to attach that. Initially I tried extending my code:
new XElement("Teaching",
new XElement("Name",
new XAttribute("Class", 0),
new XText(historyItem.Teaching[0])),
new XElement("Name",
new XAttribute("Class", 1),
new XText(historyItem.Teaching[1])),
new XElement("Name",
new XAttribute("Class", 2),
new XText(historyItem.Teaching[2])))));
But it doesn't factor in for the logical requirements. What is the sensible way to add this node using the rules indicated?
To summarize:
Create the Teaching node
if Teaching is not null
if historyItem.Classes >= 1
Add the first Name / attribute
if historyItem.Classes >= 2
Add the second Name / attribute
if historyItem.Classes == 3
Add the third Name / attribute
So if the Teaching element is null I want an empty Teaching element in XML.
Making sense?
You can take advantage of the fact that null nodes are ignored, so make a condition where you pass null when the condition is not met.
Here is the basic idea:
new XElement("Teaching",
historyItem.Classes >= 1 ? new XElement("Name",
new XAttribute("Class", 0),
new XText(historyItem.Teaching[0])) : null,
After reading the comment, it appears that you don't even need a conditional and could do the following:
historyItem.Teaching != null ? new XElement("Teaching",
new XElement("Name",
new XAttribute("Class", historyItem.Classes - 1),
new XText(historyItem.Teaching[historyItem.Classes - 1]))
), null));
I thought I would add this as an alternative to the initial answer which goes about things slightly differently. An alternative to using one large chain and factoring in the logic requirements for the Teaching node:
XElement historyWeek = new XElement("W" + record.Date.ToString("yyyyMMdd"));
if(historyItem.Meeting)
{
historyWeek.Add(new XElement("Chairman", historyItem.Chairman),
new XElement("AuxCounsellor1", historyItem.AuxCounsellor1),
new XElement("AuxCounsellor2", historyItem.AuxCounsellor2),
new XElement("VideoConferenceHost", string.Empty),
new XElement("VideoConferenceCohost", string.Empty),
new XElement("PrayerOpen", historyItem.PrayerOpen),
new XElement("PrayerClose", historyItem.PrayerClose),
new XElement("CBSConductor", historyItem.CBSConductor),
new XElement("CBSReader", historyItem.CBSReader),
new XElement("ReviewQuestion", string.Empty));
XElement historyTalkItems = new XElement("Items",
new XAttribute("ItemCount", historyItem.TalkItems.Count));
foreach (var talkItem in historyItem.TalkItems)
{
historyTalkItems.Add(new XElement("Item",
new XElement("Name", talkItem.Name),
new XElement("Theme", talkItem.Theme),
new XElement("Method", talkItem.Method)));
}
historyWeek.Add(historyTalkItems);
XElement historyTeaching = new XElement("Teaching");
if(historyItem.Teaching.Any())
{
for (int iClass = 0; iClass < historyItem.Classes; iClass++)
{
historyTeaching.Add(new XElement("Name",
new XAttribute("Class", iClass),
new XText(historyItem.Teaching[iClass])));
}
}
historyWeek.Add(historyTeaching);
}
xdoc.Root.Add(historyWeek);

Generating Xelement from list of object

My Code
IList<Person> people=new List<Person>();
people.Add(new Person{Id=1,Name="Nitin"});
IList<decimal> my=new List<decimal>(){1,2,3};
IList<int> your=new List<int>(){1,2,3};
XElement xml = new XElement("people",
from p in people
select new XElement("person", new XAttribute("Id", "Hello"),
new XElement("id", p.Id),
new XElement("Mrp", my.Contains(1) ? string.Join(",",my):"Nitin"),
new XElement("Barcode", Form1.GetStrings(1).Select(i => new XElement("Barcode", i)))
));
MessageBox.Show(xml.ToString());
GetStrings only returns int from 1 to 4
Output
<people>
<person Id="Hello">
<id>1</id>
<Mrp>1,2,3</Mrp>
<Barcode>
<Barcode>1</Barcode>
<Barcode>2</Barcode>
<Barcode>3</Barcode>
<Barcode>4</Barcode>
</Barcode>
</person>
</people>
But I want output as
<people>
<person Id="Hello">
<id>1</id>
<Mrp>1,2,3</Mrp>
<Barcode>1</Barcode>
<Barcode>2</Barcode>
<Barcode>3</Barcode>
<Barcode>4</Barcode>
</person>
</people>
Any Solutions
Then instead of this:
new XElement("Barcode", Form1.GetStrings(1).Select(i => new XElement("Barcode", i)))
Use your query directly like this, don't create an extra Barcode element:
Form1.GetStrings(1).Select(i => new XElement("Barcode", i))
Then your code should look like this:
XElement xml = new XElement("people",
from p in people
select new XElement("person", new XAttribute("Id", "Hello"),
new XElement("id", p.Id),
new XElement("Mrp", my.Contains(1) ? string.Join(",",my):"Nitin"),
Form1.GetStrings(1).Select(i => new XElement("Barcode", i))
));
That will give you the expected output.
Try this :
XElement xml = new XElement("people",
from p in people
select new XElement("person", new XAttribute("Id", "Hello"),
new XElement("id", p.Id),
new XElement("Mrp", my.Contains(1) ? string.Join(",",my):"Nitin"),
Form1.GetStrings(1).Select(i => new XElement("Barcode", i))
));

how to display right xml format

I am trying to create an xml doc with a prefix g:
c#
static void Main(string[] args)
{
XNamespace g = "g:";
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement( g+"Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("street","this street"))
)
);
Console.WriteLine(contacts);
}
instead it shows up with:
..<contacts>
<contact>
<name xmlns="g:">
...
XNamespace g = "http://somewhere.com";
XElement contacts =
new XElement("Contacts", new XAttribute(XNamespace.Xmlns + "g", g),
new XElement("Contact",
new XElement( g+"Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("street","this street"))
)
);
OUTPUT :
<Contacts xmlns:g="http://somewhere.com">
<Contact>
<g:Name>Patrick Hines</g:Name>
<Phone>206-555-0144</Phone>
<Address>
<street>this street</street>
</Address>
</Contact>
</Contacts>
Your code is working fine for me (output is):
<Contacts>
<Contact>
<Name xmlns="g:">Patrick Hines</Name>
<Phone>206-555-0144</Phone>
<Address>
<street>this street</street>
</Address>
</Contact>
</Contacts>
if you still can not get the correct output then you can try this:
new XDocument(
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("street","this street"))
)
)
).Save("foo.xml");
XDocument is in System.Xml.Linq namespace. So, at the top of your code file, add:
using System.Xml.Linq;
Then you can write the data to your file the following way:
XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new System.Xml.Linq.XElement("Contacts"),
new XElement("Contact",
new XElement("Name", c.FirstOrDefault().DisplayName),
new XElement("PhoneNumber", c.FirstOrDefault().PhoneNumber.ToString()),
new XElement("Email", "abc#abc.com"));
doc.Save(...);

How to build an XDocument with a foreach and LINQ?

I can use XDocument to build the following file which works fine:
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null),
new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
),
new XElement(_singularCamelNotation,
new XElement("id", "2"),
new XElement("whenCreated", "2008-12-31")
)
)
);
However, I need to build the XML file by iterating through a collection like this:
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null));
foreach (DataType dataType in _dataTypes)
{
XElement xelement = new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
));
xdoc.AddInterally(xelement); //PSEUDO-CODE
}
There is Add, AddFirst, AddAfterSelf, AddBeforeSelf, but I could get none of them to work in this context.
Is an iteration with LINQ like this possible?
Answer:
I took Jimmy's code suggestion with the root tag, changed it up a bit and it was exactly what I was looking for:
var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(_pluralCamelNotation,
_dataTypes.Select(datatype => new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
))
)
);
Marc Gravell posted a better answer to this on this StackOverflow question.
You need a root element.
var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("Root",
_dataTypes.Select(datatype => new XElement(datatype._pluralCamelNotation,
new XElement(datatype._singlarCamelNotation),
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
))
)
);
If I'm not mistaken, you should be able to use XDocument.Add():
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null));
foreach (DataType dataType in _dataTypes)
{
XElement xelement = new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
));
xdoc.Add(xelement);
}
I know it's very very old post but I stumbled across this today trying to solve the same problem. You have to add the element to the Root of the document:
xdoc.Root.Add(xelement);
What's wrong with the simple Add method?

Categories

Resources