【发布时间】:2021-11-30 13:54:03
【问题描述】:
这似乎是一个简单的问题,但我似乎无法着手制定可行的解决方案。最终目标是更改下面突出显示的ConstantValue 元素的值。我的策略是找到Component 节点并从那里向下钻取。问题是不断返回一个空值,我不知道为什么。下面是我正在使用的代码,我正在使用的 xml。任何提示都会很棒。
XDocument xmlDoc = XDocument.Parse(str);
var items = xmlDoc.Descendants("Component")
.Where(x => x.Attribute("Name").Value == "axesInterface")
.FirstOrDefault();
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Engineering version="V17" />
<DocumentInfo>
</DocumentInfo>
<SW.Blocks.FB ID="0">
<AttributeList>
<Interface><Sections></Sections></Interface>
<MemoryLayout>Optimized</MemoryLayout>
<MemoryReserve>100</MemoryReserve>
<Name>EM00_CM01_Warp1</Name>
<Number>31650</Number>
<ProgrammingLanguage>LAD</ProgrammingLanguage>
<SetENOAutomatically>false</SetENOAutomatically>
</AttributeList>
<ObjectList>
<SW.Blocks.CompileUnit ID="4" CompositionName="CompileUnits">
<AttributeList>
<NetworkSource>
<FlgNet xmlns="http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4">
<Parts>
<Access Scope="GlobalVariable" UId="27">
<Symbol>
<Component Name="HMIAxisCtrl_Interface" />
<Component Name="axesInterface" AccessModifier="Array">
<Access Scope="LiteralConstant">
<Constant>
<ConstantType>DInt</ConstantType>
<ConstantValue>0</ConstantValue>
</Constant>
</Access>
</Component>
</Symbol>
</Access>
</Parts>
</FlgNet>
</NetworkSource>
</AttributeList>
</SW.Blocks.CompileUnit>
</ObjectList>
</SW.Blocks.FB>
</Document>
【问题讨论】:
-
如果您包含格式良好的 XML 来说明您正在尝试做什么(作为文本,格式化为 代码我>)。这样,人们就可以获取您的 XML 并使用它。包括代码(XML 不是完全代码,但它足够接近)作为文本总是比作为图片更好
-
您需要像@Charlieface 在他的回答中那样处理 FlgNet 元素上的命名空间