1、在哪里使用代码修改Pro的配置
首先介绍下Pro API中的ConfigurationManager类的void OnUpdateDatabase(XDocument database)方法。该方法提供途径让开发者修改Pro的配置,改方法的参数database代表所有配置整合成的XML内容,调试模式下看该XML会发现我们定义的delete、update节点都没了,说明pro自己根据所有的配置融合成一个新的XML,就是这里的database参数。我们在此可以对其进行最后的修改,之后Pro会根据此xml内容生成backstage、tab、panel、button等界面元素。
2、LINQ
语言集成查询 (LINQ) 是一系列直接将查询功能集成到 C# 语言的技术统称。我觉得LINQ最方便的就是使用起来十分灵活,充分利用了C#的语法特性:使用select、from、where等关键字构建查询语句、使用System.Linq命名空间下的查询方法结合Lamda表达式进行查询。
3、LINQ to XML简介
如果感兴趣可以参考:Linq to xml
通常情况下c#操作Xml的方式有:
- 使用C#的DOM操作XML,即我们常用的XmlDocument、XmlNode、XmlElement类;
- 使用Linq to XML进行XML操作
LINQ to XML 提供使用 .NET 语言集成查询 (LINQ) Framework 的内存中 XML 编程接口。最重要的优势是它与 语言集成查询 (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元素和属性的集合。 LINQ to XML 的另一个优势是通过将查询结果用作XElement和XAttribute对象构造函数的参数,实现了一种功能强大的创建 XML 树的方法。
OnUpdateDatabase(XDocument database)方法的参数为XDocument类型,那么按F12我们发现该类在System.Xml.Linq命名空间下。
4、使用LINQ to XML操作Pro配置
4.1 DAML Schame
首先要了解Pro DAML的架构。
4.2 查询
查询所有的tab节点
var nsp = database.Root.Name.Namespace;
var tabElements = from seg in database.Root.Descendants(nsp + "tab") select seg;
查询自定义的Module:注意一点,自定义的是insertmodule,而database参数内容是module节点,证明上面的说法,此处的database参数是pro处理所有配置得来的。
使用查询表达式语法:
var modules = from seg in database.Root.Descendants(nsp + "module")
where seg.Attribute("id").Value.Equals("TDProAppModule_Module")
select seg;
var moduleTDProcofig = modules.ToList()[0];
使用方法语法:
var nsp = database.Root.Name.Namespace;
var moduleTDProcofig = database.Root.Descendants(nsp + "module").First(
ele =>
ele.Attribute("id").Value.Equals("TDProAppModule_Module"));
moduleTDProcofig.Remove();
4.3 更新
更新节点属性
var modules = from seg in database.Root.Descendants(nsp + "module") where seg.Attribute("id").Value.Equals("TDProAppModule_Module") select seg;
var moduleTDProcofig = modules.ToList()[0];
moduleTDProcofig.Attribute("condition").Value = "test2";
4.4 删除
移除节点
var modules = from seg in database.Root.Descendants(nsp + "module") where seg.Attribute("id").Value.Equals("TDProAppModule_Module") select seg;
var moduleTDProcofig = modules.ToList()[0];
moduleTDProcofig.Remove();
4.5 构造和新增
新增元素
Console.WriteLine("==========元素创建==============");
XElement srcTree = new XElement("Root",
new XElement("Element", 1),
new XElement("Element", 2),
new XElement("Element", 3),
new XElement("Element", 4),
new XElement("Element", 5)
);
XElement xmlTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
from el in srcTree.Elements()
where (int)el > 2
select el
);
Console.WriteLine(xmlTree);
带命名空间
public void CreateWithNamespace()
{
Console.WriteLine("==========带命名空间填写==============");
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
}