1

I have this xml enter image description here

This is the xml:

<?xml version="1.0"?>
<model xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="id- 
5f3a493ba7067b8a30bfbf6b" 
 xmlns="http://www.opengroup.org/xsd/archimate/3.0/">
<name xml:lang="EN"></name>
<views>
<diagrams>
  <view identifier="id-4bad55d7-9619-4f21-bcc2-47ddb19d57b3">
      <name xml:lang="EN">1shape</name>
    <node xmlns:q1="" xsi:type="q1:Shape" identifier="id-97d9fcda-f478-4564-9abd-e2f544d2e292" x="10" y="30" w="1" h="1" nameinternal="BD_shapes_av_Square" shapeId="5bc59d14ec9d2633e8ea102e" angle="0" isgroup="False" alignment="" textalign="0" size="696 360">
      <label xml:lang="EN" />
      <style>
        <fillColor r="102" g="170" b="215" />
        <font name="Lato" size="13">
          <color r="255" g="255" b="255" />
        </font>
      </style>
    </node>
    <node xmlns:q2="" xsi:type="q2:Shape" identifier="id-3b754535-530f-49d2-9e7b-113df0659af9" x="226" y="114" w="1" h="1" nameinternal="BD_shapes_av_Coffee" shapeId="5dad5a884527ecc5c8c4871b" angle="0" isgroup="False" alignment="" textalign="0" size="52.3 72">
      <label xml:lang="EN" />
      <style>
        <fillColor r="102" g="45" b="145" />
        <font name="Lato" size="13">
          <color r="255" g="255" b="255" />
        </font>
      </style>
    </node>
  </view>
</diagrams>

I need to check if the node xsi:type attribute value is Shape or not. I have the xml loaded in XDocument I've tried to get to the node element

 xDocument.Descendants("views").Attributes("xsi:type");

If I use

xDocument.Root.Element("views"); - it returns null

Thanks in advance!

2

1 Answer 1

5

There are a couple of problems here. Firstly, you're currently looking for an element called views which isn't in an XML namespace.

Your XML doesn't contain any such elements - this part of your root element:

xmlns="http://www.opengroup.org/xsd/archimate/3.0/"

... means that that's the default namespace for descendants, including views.

Fortunately LINQ to XML makes it very easy to work with namespaces. You just need:

XNamespace ns = "http://www.opengroup.org/xsd/archimate/3.0/";
XElement views = xDocument.Root.Element(ns + "views");

However, that doesn't have any attributes on it. It looks like you're really trying to find the attributes of the node elements. Again, you'd want to get the namespaces right too:

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var attributes = views.Descendants(ns + "node").Attributes(xsi + "type");

Or iterating over the nodes and checking the values:

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
foreach (var node in views.Descendants(ns + "node"))
{
    var type = (string) node.Attribute(xsi + "type");
    if (type is string && type.EndsWith(":Shape"))
    {
        ...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.