M
M
mletov2018-03-07 21:37:53
C++ / C#
mletov, 2018-03-07 21:37:53

How to edit XML file with namespace prefixes via Linq to XML?

Please, prompt: It is
necessary to create an XML file with tags of this kind.
<mecp:RoomCount part="15.2.6">3</mecp:RoomCount>
When I create a file from scratch, there are no problems.
Adding a namespace to the root element

foreach (KeyValuePair<string, XNamespace> item in dctNameSpace)
                                    {
                                        _xNode.Add(new XAttribute(XNamespace.Xmlns + item.Key, item.Value.NamespaceName));
                                    }

Then I create elements
xWorkDay.Add(new XElement(mecdv + "DayOfWeek", new XAttribute("part", "1.3.1"), item.WeekDay));

everything is fine.
But here it was necessary to edit the existing xml file with a ready-made structure and given namespaces.
And exactly the same adding code instead of
<mecp:RoomCount>3</mecp:RoomCount>
Adds a T tag
<RoomCount xmlns="fdsfsfdfgvdfg">3</RoomCount>
, instead of a prefix, inserts an attribute of the xmlns namespace.
I tried to remove the namespace attributes from the root element and re-register.
Then, on the contrary, new attributes are created normally with prefixes, and the previously existing ones are replaced by xmnls
Did according to the documentation
https://docs.microsoft.com/en-us/dotnet/csharp/pro...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mletov, 2018-03-08
@mletov

I decided. It was not necessary to delete/recreate namespaces, but to get the existing ones and create elements based on them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question