1

Topic: How to work with XDocument without explicit instructions Namespace

I receive XML which Elements are in some namespace:




<data xmlns = "namespace__"> 
  <Name> a name </Name> 
  <ID/> 
</data> 



I need to work with the document in the normal image, without constant instructions namespace:



XDocument xDoc = XDocument. Parse (inputString); 
String name = xDoc. Root. Element ("Name").Value; 
//later processing 



How it becomes?

2

Re: How to work with XDocument without explicit instructions Namespace

Hello, the Anonymous author, you wrote:

I Receive XML which Elements are in some namespace:
I need to work with the document in the normal image, without constant instructions namespace:
How it becomes?


And namespace to you it is known? Business is simple in saving or in something the friend?

3

Re: How to work with XDocument without explicit instructions Namespace

_FR> And namespace to you it is known?


Namespace it is not known, but he can be derived:

XNamespace xNam = xDoc. Root. Name. Namespace;

_FR> Business is simple in saving or in something the friend?

Yes, I am am irritated fairly with type record:

String name = xDoc. Root. Element (xNam + "Name").Value;

4

Re: How to work with XDocument without explicit instructions Namespace

_FR> And namespace to you it is known? Business is simple in saving or in something the friend?


As standard means did not find, it was turned out by means of methods of extensions:




public static class Extensions { 
    public static XElement ToX (this XElement xParent, String name) { 
        return xParent. Element (xParent. Name. Namespace + name); 
    }

    public static IEnumerable <XElement> ToXs (this XElement xParent, String name) { 
        return xParent. Elements (xParent. Name. Namespace + name); 
    }
}

5

Re: How to work with XDocument without explicit instructions Namespace

Hello, the Anonymous author, you wrote:

_FR>> And namespace to you it is known?
Namespace it is not known, but he can be derived:



XNamespace xNam = xDoc. Root. Name. Namespace; 


_FR>> Business is simple in saving or in something the friend?
Yes, I am am irritated fairly with type record:



String name = xDoc. Root. Element (xNam + "Name").Value; 




var xname = doc. Root. Name. Namespace. GetName ("Name"); 
var name = (string) doc. Root. Element (xname); 



For,  lines in an any way it is not healthy, and to get a constant of type of a line or a variable of type XName from the syntactic point of view not so important.


Other variant, semihacker to walk on all documents and to replace a name with only local name.


, it is better  than itself to that namespace there is an integral part of a name and if it is set, not .

6

Re: How to work with XDocument without explicit instructions Namespace

Hello, the Anonymous author, you wrote:

_FR>> And namespace to you it is known? Business is simple in saving or in something the friend?
As standard means did not find, it was turned out by means of methods of extensions:




public static class Extensions { 
public static XElement ToX (this XElement xParent, String name) { 
return xParent. Element (xParent. Name. Namespace + name); 
} 

public static IEnumerable <XElement> ToXs (this XElement xParent, String name) { 
return xParent. Elements (xParent. Name. Namespace + name); 
} 
} 



Quite sensibly for the specific scenario. Only names to pick up more clearly, check on null and  not lines to concatenate, and to receive normal XName and with it to climb in Element (s).