C#: How to get the name (with prefix) from XElement as string?
Asked Answered
A

5

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
Audwin answered 17/6, 2011 at 15:0 Comment(0)
A
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 :)

Audwin answered 21/6, 2011 at 8:20 Comment(0)
A
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
Accrescent answered 17/6, 2011 at 17:25 Comment(1)
That looks good, if I was using an XmlElement. But since I am using an XElement it doesn't work :)Audwin
E
0

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

Ehtelehud answered 17/6, 2011 at 15:19 Comment(7)
I don't have any property Prefix on the XElement. It would be a very nice solution if I had though :)Audwin
Which version of the framework are you using? MSDN shows it having that property.Ehtelehud
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.Audwin
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.Ehtelehud
Well your suggestion was still just what I wished for ;)Audwin
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...Ehtelehud
No that would not work when running through XDocument stuff, such as Save.Duffey
M
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>
Mcnair answered 17/6, 2011 at 15:25 Comment(1)
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.Audwin
D
-1

This will return prefix from XElement:

myElement.GetPrefixOfNamespace(node.Name.Namespace);
Dowden answered 15/6, 2013 at 22:14 Comment(1)
No it does not, it returns the URI part of the namespace, not the prefix.Loyalist

© 2022 - 2024 — McMap. All rights reserved.