Add a new XElement to an existing XElement, if it does not already exist

How to check if an element exists in a given element before trying to add it?

Background . I have XDocument Xone that contains a child Flowers, which subsequently contains a series of elements, each of which is called Flower. Each Floweralready has 2 children, and I would like to add a third element with a name Price. However, I want to check and make sure that the element Flowerdoes not yet have an element for Price. How should I do it? Do I even need to check?

+3
source share
1 answer

XElement HasElements, , , - .

...

XNamespace ns = "http://mynamespace.com";
bool hasPrice = flowerElement.Element(ns + "Price") == null;

.., , . , .

. - XML , Namespace.None ns.

+5

Source: https://habr.com/ru/post/1730642/


All Articles