Assistance with how to properly create an xml file in c# - 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")
));
}

Related

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);

LINQ to XML DOM (making a modifed Tree easily)

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());

How to add elements in an XML file?

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);

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(...);

csv to xml not adding xElement if XAttribute = ""

I've create an xml file from a csv file like this
private void button2_Click(object sender, EventArgs e)
{
String[] FileContent = File.ReadAllLines(csvPathFile);
String XMLNS = "";
int idCountProduct = 1;
XElement Inv = new XElement("data",
from items in FileContent
let fields = items.Split(';')
select new XElement("category",
new XAttribute("idCategory", fields[0]),
new XAttribute("CategoryName", fields[1]),
new XElement("products",
new XElement("product",
new XAttribute("IdProduct", idCountProduct++),
new XAttribute("Rif", fields[0]),
new XAttribute("ProductName", fields[2]),
new XElement("products",
new XElement("product",
new XAttribute("IdProduct", idCountProduct++),
new XAttribute("Rif", fields[0]),
new XAttribute("ProductName", fields[3]),
new XElement("products",
new XElement("product",
new XAttribute("IdProduct", idCountProduct++),
new XAttribute("Rif", fields[0]),
new XAttribute("ProductName", fields[4]));
File.WriteAllText(xmlPathFile, XMLNS + Inv.ToString());
}
this is my csv file
1;Beverages;Lemon Juice;;Orange Juice
this is the xml file i want to create
<data>
<category idCategory="1" CategoryName= "Beverages">
<products>
<product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
<product IdProduct="2" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
</data>
and this is the xml file i obtain
<data>
<categories>
<category idCategory="1" CategoryName= "Beverages">
<products>
<product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
<product IdProduct="2" Rif="1" ProductName= "" />
<product IdProduct="3" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
<categories/>
</data>
How can I avoid adding a product if ProductName is not assigned?
Add a filter to your LINQ query to filter out empty products:
where !String.IsNullOrEmpty(fields[4])
just after this:
let fields = items.Split(';')
Anyway if your condition is so simple you do not even need it and you may filter out entries from the Split instruction itself:
let fields = items.Split(';', StringSplitOptions.RemoveEmptyEntries)
EDIT
Your code is pretty fixed so...are you sure you need to use LINQ and LINQ-to-XML? I guess it'll be even more readable anyway...write this:
XElement data = new XElement("data");
XDocument document = new XDocument(data);
int counter = 0;
foreach (string entry in File.ReadAllLines(csvPath))
{
string[] fields = entry.Split(new char[] { ';' },
StringSplitOptions.RemoveEmptyEntries);
XElement category = new XElement("category",
new XAttribute("idCategory", fields[0]),
new XAttribute("CategoryName", fields[1]));
data.Add(category);
XElement products = new XElement("products");
category.Add(products);
for (int i = 2; i < fields.Length; ++i)
{
products.Add(new XElement("product",
new XAttribute("IdProduct", counter++),
new XAttribute("Rif", fields[0]),
new XAttribute("ProductName", fields[i])));
}
}
document.Save(xmlPath);
}

Categories

Resources