I'm creating a new XML document and as part of doing this I need to create a new XElement named tag with an XAttribute called ref which has a value given to it from stringTags
I can do this for 1 item but in some cases I need to add multiple.
What I have so far is:
new XElement("tags",
new XElement("tag",
new XAttribute("ref", stringTags))
)
But what I need is something like
new XElement("tags",
new XElement("tag",
foreach (string element in list)
{
new XAttribute("ref", stringTags)
}
));
But this doesn't work.
Can anyone suggest a way of doing this - the foreach loop gives me to correct output when I place it outwith the creation of the XML doc.
// Adding the Attributes to the Dictionary as Key values pairs Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("ref", "xml");
list.Add("Id", "123");
XElement chileElement = new XElement("tag");
foreach (var item in list) // Adding each attribute to the child element
{
XAttribute attribute = new XAttribute(item.Key, item.Value);
chileElement.Add(attribute);
}
XElement elem = new XElement("tags", chileElement);
XElement elem = new XElement("tags");
foreach (var item in list)
{
XElement childElement = new XElement("tag");
XAttribute attribute = new XAttribute("ref", item);
childElement.Add(attribute);
elem.Add(childElement);
}
Works brilliantly!
)),
new XElement("categories",
new XElement("category",
new XAttribute("ref", "1"))
),
// OLD CODE THAT ONLY WORKED FOR ONE TAG
//new XElement("tags",
// new XElement("tag",
// new XAttribute("ref", tags))
// ),
// NEW Element which populates multiple tags.
new XElement(elem)
Member
1 Points
7 Posts
new XElement, foreach loop issue.
May 15, 2015 04:34 AM|mantaii|LINK
I'm creating a new XML document and as part of doing this I need to create a new XElement named tag with an XAttribute called ref which has a value given to it from stringTags
I can do this for 1 item but in some cases I need to add multiple.
What I have so far is:
But what I need is something like
But this doesn't work.
Can anyone suggest a way of doing this - the foreach loop gives me to correct output when I place it outwith the creation of the XML doc.
Foreach xml
Star
9021 Points
2415 Posts
Re: new XElement, foreach loop issue.
May 15, 2015 05:05 AM|Lokesh B R|LINK
Hi,
try this code
Foreach xml
Member
1 Points
7 Posts
Re: new XElement, foreach loop issue.
May 15, 2015 07:50 AM|mantaii|LINK
Hi,
Thanks for the quick reply.
I should have probably explained a bit more.
I have a list of the tags and I've tried to use your suggestion -
The output I am trying to get is :
I import all the tags from a file which are in a list just now, which is why I then separate it.
I get an error saying "Duplicate Attribute" on line childElement.Add(attribute)
Foreach xml
Star
9021 Points
2415 Posts
Re: new XElement, foreach loop issue.
May 15, 2015 07:55 AM|Lokesh B R|LINK
Hi,
Here is the sample for your requirement.
Foreach xml
Member
1 Points
7 Posts
Re: new XElement, foreach loop issue.
May 15, 2015 08:38 AM|mantaii|LINK
Great! That has solved the issue! Thank you!
I added a bit extra to make it populate my file.
Works brilliantly!
Foreach xml