How to create XmlElement attributes with prefix?

前端 未结 4 1383
不知归路
不知归路 2020-12-06 10:46

I need to be able to define an attribute with a prefix in a xml element.

For instance...


         


        
4条回答
  •  感动是毒
    2020-12-06 11:47

    Since my search kept taking me here, I'll answer this for XElement. I don't know if this solution is also valid for XmlElement, but it will hopefully at least help others using XElement, who end up here.

    Based on this I added xml:space="preserve" to all data-nodes in some template, before looking up and adding their contents. It's weird code IMO (I would prefer three parameters as shown above, but it does the job:

     foreach (XElement lElement in root.Descendants(myTag))
     {
          lElement.Add(new XAttribute(root.GetNamespaceOfPrefix("xml") + "space", "preserve"));
     }
    

提交回复
热议问题