1

我正在尝试编写一个读取 .vsdx 文件的 C# 应用程序。下面的 XML 是我的一个文件中的页面。只有少数Shape元素具有NameandNameU属性。有谁知道这是为什么或如何解决?下面所有Shape没有Name属性的元素都应该是Object生命线。

<?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">
4

1 回答 1

0
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);
于 2014-01-08T02:45:42.313 回答