How to set namespace on XDocument

I need to acсess xml file. But xml have base namespace whith prefix m:

This is my code, but it do not working, write NullRefference exeptions:

var fileКс = XDocument.Load(somePath);
        var allDescrioptions = fileКс.Root.Element("formulas").Elements("formula").ToList();

This is part of xml file:

    <?xml version="1.0" encoding="utf-8" ?>
<m:math xmlns:m="http://www.kontur-extern.ru/ФНС 4.0/math.xsd">
    <m:formulas>
        <m:formula target="@ПрибУб" match="/Файл/Документ/Прибыль/РасчНал" source="Лист 02/стр.060">
        </m:formula>
</m:formulas>
</m:math>

I think need to specify namespace, but I don't know how

Upvotes: 0

Views: 180

Answers (1)

har07
har07

Reputation: 89285

You can use XNamespace as follow :

XNamespace m = "http://www.kontur-extern.ru/ФНС 4.0/math.xsd";
var fileКс = XDocument.Load(somePath);
var allDescrioptions = fileКс.Root.Element(m+"formulas").Elements(m+"formula").ToList();

Upvotes: 2

Related Questions