1

If string xml; contains following xml as text

<table xmlns="http://www.mynamespace.org/standard">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table> 

I want to find the value of xmlns attribute from this string. In this example, I want to find a value like

string namespace =? 

and namespace should contain value "http://www.mynamespace.org/standard".

How this can be found effectively?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Gurpreet Kailey
  • 567
  • 9
  • 26
  • 4
    Have you tried any xml parser? – Mark Baijens Apr 12 '21 at 10:37
  • XML works with nodes, not strings. If you use any XML parser you'll be able to check a node's namespace. Whether you use XmlSerializer, XmlDocument, XmlReader or XDocument, you can check a node's namespace – Panagiotis Kanavos Apr 12 '21 at 10:38
  • 1
    @PanagiotisKanavos minutiae: `XmlSerializer` really wouldn't be a good choice here - it is fussy about namespaces being known in advance and matching, unless you *basically* tell it to deserialize into an `XmlElement` or something – Marc Gravell Apr 12 '21 at 10:44
  • I have tried but sometimes in XML file I am getting such text(format/encoding of text) that, and XDocument.Parse gives the following error "Data at the root level is invalid. Line 1, position 1." going further, the solution provided here does not always work with different files https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1 – Gurpreet Kailey Apr 12 '21 at 10:48

1 Answers1

3
var doc = XDocument.Parse(xml);
var ns = doc.Root.Name.Namespace;

or similar with XmlDocument, XmlReader, etc. If you only care about the root element, XmlReader will be the most efficient (as you can stop reading at the first node). For example:

using var reader = XmlReader.Create(source);
reader.MoveToContent();
var ns = reader.NamespaceURI;

(if your xml is in a string, then using var source = new StringReader(xml); can help with that)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for the quick response Marc, I have implemented the same but sometimes in XML file I am getting such text(format/encoding of text) that, and XDocument.Parse gives the following error "Data at the root level is invalid. Line 1, position 1." going further, the solution provided here does not always work with different files https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1 – Gurpreet Kailey Apr 12 '21 at 10:47
  • @GurpreetKailey It should, though. Of course, your input files may be broken (i.e. invalid xml, not well-formed ... or sometimes even BOMs can hurt). – Fildor Apr 12 '21 at 10:54
  • 1
    @GurpreetKailey honestly, I'm going to assume that the framework is right, and that your xml is in fact: broken. Do you have an example of a broken file? Note: as noted in question you link to, the underlying case could be a BOM in the wrong place; *most* of the time, BOMs should be handled entirely silently and automatically; it may also help to specify the encoding explicitly when opening the file – Marc Gravell Apr 12 '21 at 11:00
  • Is it possible to find it using regular expression? – Gurpreet Kailey Apr 12 '21 at 12:04
  • @GurpreetKailey if it is a BOM? no, not really; a BOM isn't part of the payload, and a regex looks at a payload; and to load the payload (correctly) you need to use the right encoding; if that isn't currently working, you need to fix *that*, not try to hack around data that is by definition *already corrupt* (at least partially) – Marc Gravell Apr 12 '21 at 12:06