Some shapes missing Name attribute in XML

405 views Asked by At

I am trying to write a C# app which reads in a .vsdx file. The XML below is the page from one of my files. Only a few Shape elements have a Name and NameU attribute. Does anyone know why this is or how it can be fixed? All of the Shape elements below which do not have Name attributes should be Object lifelines.

<?xml version="1.0" encoding="UTF-8"?>
-<PageContents xml:space="preserve" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.microsoft.com/office/visio/2012/main">
-<Shapes>
+<Shape Master="7" Type="Group" Name="Object lifeline" NameU="Object lifeline" ID="1">
+<Shape Master="7" Type="Group" ID="6">
+<Shape Master="7" Type="Group" ID="11">
+<Shape Master="7" Type="Group" ID="16">
+<Shape Master="7" Type="Group" ID="21">
+<Shape Master="7" Type="Group" ID="26">
+<Shape Master="7" Type="Group" ID="31">
+<Shape Master="8" Type="Shape" Name="Message" NameU="Message" ID="36">
+<Shape Master="8" Type="Shape" ID="37">
+<Shape Master="9" Type="Shape" Name="Return Message" NameU="Return Message" ID="38">
+<Shape Master="7" Type="Group" Name="Object lifeline.39" NameU="Object lifeline.39" ID="39">
+<Shape Master="7" Type="Group" Name="Object lifeline.44" NameU="Object lifeline.44" ID="44">
+<Shape Master="8" Type="Shape" ID="49">
+<Shape Master="8" Type="Shape" ID="51">
+<Shape Master="9" Type="Shape" ID="52">
+<Shape Master="11" Type="Shape" Name="Activation" NameU="Activation" ID="53">
+<Shape Master="8" Type="Shape" ID="54">
1

There are 1 answers

1
Selman Genç On
var xDoc = XDocument.Load(path);
var elements = (from e in xDoc.Descendants("Shape")
                    where (string)e.Attribute("Name") == null
                      select e).ToList();
foreach (var item in elements)
{
    item.Add(new XAttribute("Name", "Object lifeline");
}
xDoc.Save(path);