locked
new XElement, foreach loop issue. RRS feed

  • Question

  • User-865231019 posted

    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. 

    Friday, May 15, 2015 4:34 AM

Answers

  • User1577371250 posted

    Hi,

    Here is the sample for your requirement.

                string tags = "Umbraco,MVC,IoC,Autofac";
    
                IList<string> list = new List<string>(tags.Split(new string[] { "," }, StringSplitOptions.None));
                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);
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 7:55 AM

All replies

  • User1577371250 posted

    Hi,

    try this code

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

    Friday, May 15, 2015 5:05 AM
  • User-865231019 posted

    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 -

      IList<string> list = new List<string>(tags.Split(new string[]{","}, StringSplitOptions.None));
    
                    XElement childElement = new XElement("tag");
                    foreach (var item in list)
                    {
                        XAttribute attribute = new XAttribute("ref", item);
                        childElement.Add(attribute);
                    }

    The output I am trying to get is :

       <tags>
            <tag ref="Umbraco" />
            <tag ref="MVC" />
            <tag ref="IoC" />
            <tag ref="Autofac" />
          </tags>

    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)

    Friday, May 15, 2015 7:50 AM
  • User1577371250 posted

    Hi,

    Here is the sample for your requirement.

                string tags = "Umbraco,MVC,IoC,Autofac";
    
                IList<string> list = new List<string>(tags.Split(new string[] { "," }, StringSplitOptions.None));
                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);
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 7:55 AM
  • User-865231019 posted

    Great! That has solved the issue! Thank you! 

    I added a bit extra to make it populate my file. 

     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)



    Friday, May 15, 2015 8:38 AM