6

This might be duplicate since my question seems so trivial, but I haven't been able to find the answer here on stackoverflow.com.

I have an XElement with data like this:

<abc:MyElement>My value</abc:MyElement>

Question: How do I get the complete name with prefix as a string from the XElement?

Expected result:

abc:MyElement
Chau
  • 5,540
  • 9
  • 65
  • 95

5 Answers5

10

My solution so far has been to use the method GetPrefixOfNamespace available in the XElement.

Though not a pretty solution, it gives me what I want:

XElement xml = new XElement(...);
string nameWithPrefix = xml.GetPrefixOfNamespace(xml.Name.Namespace) + 
                        ":" + 
                        xml.Name.LocalName;

More elegant solutions are very welcome :)

Chau
  • 5,540
  • 9
  • 65
  • 95
3

Correct I was not using the same objects as you. with LINQ namesapce you the solution is:

using System.Xml.XPath; // <-- Add this namespace.

XNamespace ci = "http://foo.com";
XElement root = new XElement(ci + "Root", new XAttribute(XNamespace.Xmlns + "abc", "http://foo.com"));
XElement childElement = new XElement(ci + "MyElement", "content");
root.Add(childElement);
var str = childElement.XPathEvaluate("name()"); // <-- Tell Xpath to do the work for you :).
Console.WriteLine(str);

prints

abc:MyElement
Rob
  • 2,618
  • 2
  • 22
  • 29
  • That looks good, if I was using an `XmlElement`. But since I am using an `XElement` it doesn't work :) – Chau Jun 20 '11 at 07:12
0

Does string.Format("{0}:{1}", XElement.Prefix, XElement.Name) not work?

therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36
  • I don't have any property `Prefix` on the `XElement`. It would be a very nice solution if I had though :) – Chau Jun 20 '11 at 07:39
  • Which version of the framework are you using? [MSDN](http://www.google.com/url?sa=t&source=web&cd=1&sqi=2&ved=0CBoQFjAA&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.xml.linq.xelement.aspx&ei=UVz_TZrfIKTViAKB5oiOBQ&usg=AFQjCNEv3mDy6Q_VL7I6P_fn3f4CEnAqhg&sig2=O1rNLKqaMVCmNxFJyYlu4g) shows it having that property. – therealmitchconnors Jun 20 '11 at 14:46
  • Can you direct me closer? I cannot find the property/method `Prefix`. I can find the method `public string GetPrefixOfNamespace(XNamespace ns)` and so far I have used that one to give me the prefix. – Chau Jun 21 '11 at 08:14
  • You are right. I don't know why I thought it was there... your solution looks like the best one to me, unless you wanted to do some XPath style LINQ queries, but that is definitely not more elegant. – therealmitchconnors Jun 21 '11 at 17:16
  • Well your suggestion was still just what I wished for ;) – Chau Jun 21 '11 at 19:50
  • This comment could potentially have been the answer, and you can't mark a comment as an answer. If this suggestion were to have fixed the questioner's problem, I would like to have been marked as the answer... – therealmitchconnors Aug 09 '12 at 17:46
  • No that would not work when running through XDocument stuff, such as Save. – fletchsod Aug 08 '17 at 13:19
0
XNamespace ci = "http://foo.com";
XElement myElement = new XElement(ci + "MyElement", "MyValue");
XElement rootElement = new XElement("root",
        new XAttribute(XNamespace.Xmlns + "abc", ci), myElement);

var str = myElement.ToString();
Console.WriteLine(str);

prints

<abc:MyElement xmlns:abc="http://foo.com">MyValue</abc:MyElement>
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I have tried to make my question more clear. I just want the prefix and the element name (`abc:MyElement`) - not the entire element with namespaces and value. – Chau Jun 20 '11 at 07:32
-1

This will return prefix from XElement:

myElement.GetPrefixOfNamespace(node.Name.Namespace);
Khalil
  • 139
  • 2
  • 1