Ghasem
Ghasem

Reputation: 15573

Adding XmlAttribute to XmlAttributeCollection

I want to create an XmlAttributeCollection and add some attributes depending on conditions. This is what I've tried.

XmlElement fishElement = doc.CreateElement(string.Empty, "fi", string.Empty);
XmlAttributeCollection attCollection = AddExtraAttributes(doc,condition);
foreach (XmlAttribute attribute in attCollection)
   {
      fishElement.Attributes.Append((XmlAttribute)attribute.Clone());
   }

private XmlAttributeCollection AddExtraAttributes(XmlDocument doc,bool condition)
{
    XmlAttributeCollection xmlAttributeCollection;
    if(condition)
     {
        XmlAttribute attribute = doc.CreateAttribute("A");
        attribute.Value = "value1";
        xmlAttributeCollection.Append(attribute);
     }
    else
     {
        XmlAttribute attribute = doc.CreateAttribute("B");
        attribute.Value = "value2";
        xmlAttributeCollection.Append(attribute);
     }
   return xmlAttributeCollection;
}

But this way, I encounter this error:

xmlAttributeCollection might not be initialized before accessing

And if i try to do this:

XmlAttributeCollection xmlAttributeCollection = new XmlAttributeCollection;

It says:

Cannot access internal constructor 'XmlAttributeCollection' here

So how can I make it works?

Update

So I've solved the problem this way

XmlAttribute newAttr = doc.CreateAttribute("genre");
newAttr.Value = "novel";
XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
attrColl.Append(newAttr);

But now everytime I call AddExtraAttributes, xmlAttributeCollection has some extra attributes and I can't set it to null (xmlAttributeCollection=null) before I run my method again. because then I can't run xmlAttributeCollection.append.

So How can I reset xmlAttributeCollection before adding attributes to it all over again?

Upvotes: 0

Views: 2504

Answers (1)

Ghasem
Ghasem

Reputation: 15573

I found a solution here

I don't know if there's a better one out there.

XmlDocument doc = new XmlDocument();
doc.LoadXml("<book ISBN='1-861001-57-5'>" +
            "<title>Pride And Prejudice</title>" +
            "</book>");      

//Create a new attribute.
XmlAttribute newAttr = doc.CreateAttribute("genre");
newAttr.Value = "novel";

//Create an attribute collection and add the new attribute 
//to the collection.
XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
attrColl.Append(newAttr);

Update

As a solution for my second problem I used a list of XmlAttributes so that I can create a new instance everytime I call the method. Now the whole code looks like this:

XmlElement fishElement = doc.CreateElement(string.Empty, "fi", string.Empty);
IEnumerable<XmlAttribute> attCollection = AddExtraAttributes(doc,condition);
foreach (XmlAttribute attribute in attCollection)
   {
      fishElement.Attributes.Append(attribute);
   }

private IEnumerable<XmlAttribute> AddExtraAttributes(XmlDocument doc,bool condition)
{
    List<XmlAttribute> xmlAttributeCollection = new List<XmlAttribute>();
    if(condition)
     {
        XmlAttribute attribute = doc.CreateAttribute("A");
        attribute.Value = "value1";
        xmlAttributeCollection.Add(attribute);
     }
    else
     {
        XmlAttribute attribute = doc.CreateAttribute("B");
        attribute.Value = "value2";
        xmlAttributeCollection.Add(attribute);
     }
   return xmlAttributeCollection;
}

Upvotes: 1

Related Questions