我有以下相当长的XML文件。我正在尝试选择一个节点并操作ist属性(@Name)。我不知道为什么,但是它以NULL异常结束。
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Engineering version="V14 SP1" />
<DocumentInfo>
<Created>2018-08-16T07:12:25.7973125Z</Created>
<ExportSetting>WithDefaults, WithReadOnly</ExportSetting>
<InstalledProducts>
<Product>
<DisplayName>Totally Integrated Automation Portal</DisplayName>
<DisplayVersion>V14 SP1 Update 6</DisplayVersion>
</Product>
<OptionPackage>
<DisplayName>TIA Portal Openness</DisplayName>
<DisplayVersion>V14 SP1 Update 6</DisplayVersion>
</OptionPackage>
<Product>
<DisplayName>SINAMICS Startdrive</DisplayName>
<DisplayVersion>V14 SP1 Update 6</DisplayVersion>
</Product>
<Product>
<DisplayName>STEP 7 Professional</DisplayName>
<DisplayVersion>V14 SP1 Update 6</DisplayVersion>
</Product>
<OptionPackage>
<DisplayName>STEP 7 Safety</DisplayName>
<DisplayVersion>V14 SP1 Update 4</DisplayVersion>
</OptionPackage>
<Product>
<DisplayName>WinCC Professional</DisplayName>
<DisplayVersion>V14 SP1 Update 6</DisplayVersion>
</Product>
<OptionPackage>
<DisplayName>SIMATIC Visualization Architect</DisplayName>
<DisplayVersion>V14 SP1</DisplayVersion>
</OptionPackage>
</InstalledProducts>
</DocumentInfo>
<SW.Blocks.FB ID="0">
<AttributeList>
<AutoNumber>true</AutoNumber>
<CodeModifiedDate ReadOnly="true">2018-08-16T07:11:59.336375Z</CodeModifiedDate>
<CompileDate ReadOnly="true">2018-08-16T07:12:12.3617656Z</CompileDate>
<CreationDate ReadOnly="true">2018-08-16T07:10:53.345164Z</CreationDate>
<DownloadWithoutReinit ReadOnly="true">false</DownloadWithoutReinit>
<HandleErrorsWithinBlock ReadOnly="true">false</HandleErrorsWithinBlock>
<HeaderAuthor />
<HeaderFamily />
<HeaderName />
<HeaderVersion>0.1</HeaderVersion>
<Interface><Sections xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v2">
<Section Name="Input" />
<Section Name="Output" />
<Section Name="InOut" />
<Section Name="Static" />
<Section Name="Temp" />
<Section Name="Constant" />
</Sections></Interface>
<InterfaceModifiedDate ReadOnly="true">2018-08-16T07:10:53.345164Z</InterfaceModifiedDate>
<IsConsistent ReadOnly="true">true</IsConsistent>
<IsIECCheckEnabled>false</IsIECCheckEnabled>
<IsKnowHowProtected ReadOnly="true">false</IsKnowHowProtected>
<ISMultiInstanceCapable ReadOnly="true">true</ISMultiInstanceCapable>
<IsRetainMemResEnabled>false</IsRetainMemResEnabled>
<IsWriteProtected ReadOnly="true">false</IsWriteProtected>
<LibraryConformanceStatus ReadOnly="true">Fehler: Der Baustein enthält Aufrufe von Einzelinstanzen.
Warnung: Der Baustein enthält direkte Zugriffe auf Eingänge, Ausgänge, Merker, indirekte Zugriffe auf Register oder indirekte Zugriffe auf externe, zum Übersetzungszeitpunkt unbekannte Speicherbereiche.
</LibraryConformanceStatus>
<MemoryLayout>Optimized</MemoryLayout>
<MemoryReserve>100</MemoryReserve>
<ModifiedDate ReadOnly="true">2018-08-16T07:11:59.336375Z</ModifiedDate>
<Name>Baustein_29</Name>
<Number>36</Number>
<ParameterModified ReadOnly="true">2018-08-16T07:10:53.345164Z</ParameterModified>
<PLCSimAdvancedSupport ReadOnly="true">true</PLCSimAdvancedSupport>
<ProgrammingLanguage>FBD</ProgrammingLanguage>
<StructureModified ReadOnly="true">2018-08-16T07:10:53.345164Z</StructureModified>
<UDABlockProperties />
<UDAEnableTagReadback>false</UDAEnableTagReadback>
</AttributeList>
<ObjectList>
<MultilingualText ID="1" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="2" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="3" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="4" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<SW.Blocks.CompileUnit ID="5" CompositionName="CompileUnits">
<AttributeList>
<NetworkSource><FlgNet xmlns="http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v1">
<Parts>
<Access Scope="GlobalVariable" UId="21">
<Symbol>
<Component Name="INPUT_BOOL" /> //this has to modified
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="22">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="23">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="24">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="25">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="26">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="27">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="28">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="29">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="30">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="31">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="32">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="33">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="34">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="35">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="36">
<Symbol>
<Component Name="INPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="824" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="37">
<Symbol>
<Component Name="INPUT_TIME" />
<Address Area="Memory" Type="Time" BitOffset="200" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="38">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="39">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="40">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="41">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="42">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="43">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="44">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="45">
<Symbol>
<Component Name="OUTPUT_BOOL" />
<Address Area="Memory" Type="Bool" BitOffset="880" Informative="true" />
</Symbol>
</Access>
<Call UId="46">
<CallInfo Name="VAL" BlockType="FB">
<IntegerAttribute Name="BlockNumber" Informative="true">610</IntegerAttribute>
<DateAttribute Name="ParameterModifiedTS" Informative="true">2017-05-10T09:20:20</DateAttribute>
<Instance UId="47" Scope="GlobalVariable">
<Component Name="VAL_DB" />
<Address Area="DB" BlockNumber="131" BitOffset="0" Informative="true" />
</Instance>
<Parameter Name="LOCK_1" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="LOCK_2" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="LOCK_3" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="LOCK_4" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="EXT_FLT" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="LOCAL" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="LOCAL_ON" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="MAN" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="AUT" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="AUT_ON" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="FB_OP" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="FB_CL" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="ACK" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="MODE" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="FUNCTION" Section="Input" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="FB_TIME" Section="Input" Type="Time">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QCONTROL" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QFLT" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QAUTO" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QMAIN" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QSIM" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QFB_OP" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QFB_CL" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
<Parameter Name="QLOCK" Section="Output" Type="Bool">
<StringAttribute Name="InterfaceFlags" Informative="true">S7_Visible</StringAttribute>
</Parameter>
</CallInfo>
</Call>
</Parts>
<Wires>
<Wire UId="48">
<IdentCon UId="21" />
<NameCon UId="46" Name="en" />
</Wire>
<Wire UId="49">
<IdentCon UId="22" />
<NameCon UId="46" Name="LOCK_1" />
</Wire>
<Wire UId="50">
<IdentCon UId="23" />
<NameCon UId="46" Name="LOCK_2" />
</Wire>
<Wire UId="51">
<IdentCon UId="24" />
<NameCon UId="46" Name="LOCK_3" />
</Wire>
<Wire UId="52">
<IdentCon UId="25" />
<NameCon UId="46" Name="LOCK_4" />
</Wire>
<Wire UId="53">
<IdentCon UId="26" />
<NameCon UId="46" Name="EXT_FLT" />
</Wire>
<Wire UId="54">
<IdentCon UId="27" />
<NameCon UId="46" Name="LOCAL" />
</Wire>
<Wire UId="55">
<IdentCon UId="28" />
<NameCon UId="46" Name="LOCAL_ON" />
</Wire>
<Wire UId="56">
<IdentCon UId="29" />
<NameCon UId="46" Name="MAN" />
</Wire>
<Wire UId="57">
<IdentCon UId="30" />
<NameCon UId="46" Name="AUT" />
</Wire>
<Wire UId="58">
<IdentCon UId="31" />
<NameCon UId="46" Name="AUT_ON" />
</Wire>
<Wire UId="59">
<IdentCon UId="32" />
<NameCon UId="46" Name="FB_OP" />
</Wire>
<Wire UId="60">
<IdentCon UId="33" />
<NameCon UId="46" Name="FB_CL" />
</Wire>
<Wire UId="61">
<IdentCon UId="34" />
<NameCon UId="46" Name="ACK" />
</Wire>
<Wire UId="62">
<IdentCon UId="35" />
<NameCon UId="46" Name="MODE" />
</Wire>
<Wire UId="63">
<IdentCon UId="36" />
<NameCon UId="46" Name="FUNCTION" />
</Wire>
<Wire UId="64">
<IdentCon UId="37" />
<NameCon UId="46" Name="FB_TIME" />
</Wire>
<Wire UId="65">
<NameCon UId="46" Name="QCONTROL" />
<IdentCon UId="38" />
</Wire>
<Wire UId="66">
<NameCon UId="46" Name="QFLT" />
<IdentCon UId="39" />
</Wire>
<Wire UId="67">
<NameCon UId="46" Name="QAUTO" />
<IdentCon UId="40" />
</Wire>
<Wire UId="68">
<NameCon UId="46" Name="QMAIN" />
<IdentCon UId="41" />
</Wire>
<Wire UId="69">
<NameCon UId="46" Name="QSIM" />
<IdentCon UId="42" />
</Wire>
<Wire UId="70">
<NameCon UId="46" Name="QFB_OP" />
<IdentCon UId="43" />
</Wire>
<Wire UId="71">
<NameCon UId="46" Name="QFB_CL" />
<IdentCon UId="44" />
</Wire>
<Wire UId="72">
<NameCon UId="46" Name="QLOCK" />
<IdentCon UId="45" />
</Wire>
</Wires>
</FlgNet></NetworkSource>
<ProgrammingLanguage>FBD</ProgrammingLanguage>
</AttributeList>
<ObjectList>
<MultilingualText ID="6" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="7" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="8" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="9" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="A" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="B" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="C" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="D" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.CompileUnit>
<SW.Blocks.CompileUnit ID="E" CompositionName="CompileUnits">
<AttributeList>
<NetworkSource />
<ProgrammingLanguage>FBD</ProgrammingLanguage>
</AttributeList>
<ObjectList>
<MultilingualText ID="F" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="10" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="11" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="12" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="13" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="14" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="15" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="16" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.CompileUnit>
<MultilingualText ID="17" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="18" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="19" CompositionName="Items">
<AttributeList>
<Culture>en-GB</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
<MultilingualTextItem ID="1A" CompositionName="Items">
<AttributeList>
<Culture>fr-CH</Culture>
<Text />
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.FB>
</Document>
然后我尝试使用if周期修改访问对象的每个名称-属性: 我正在尝试使用以下代码:
XDocument xdoc = XDocument.Load("c:/export/" + NAME + ".xml");
xdoc.XPathSelectElement("/Document/SW.Blocks.FB/AttributeList/Name").Value = NAME;
xdoc.XPathSelectElement("/Document/SW.Blocks.FB/ObjectList/SW.Blocks.CompileUnit[1]/ObjectList/MultilingualText[1]/ObjectList/MultilingualTextItem[1]/AttributeList/Text").Value = KOMMENTAR;
//((IEnumerable<object>)xdoc.XPathEvaluate("/Document/SW.Blocks.FB/ObjectList/SW.Blocks.CompileUnit[1]/AttributeList/NetworkSource/FlgNet/Parts/Call/CallInfo/Instance/Component/@Name")).OfType<XAttribute>().Single().Value = DBNAME;
for (int i = 0; i < AccVariablen.Length; i++)
{
if (AccVariablen[i] != String.Empty)
{
int j = i + 21;
//xdoc.XPathSelectElement ("//Access["+j+"]/Symbol/Component/@Name").SetValue(AccVariablen[i]);
XElement modMethodXElement = xdoc.XPathSelectElement(" / Document / SW.Blocks.FB / ObjectList / SW.Blocks.CompileUnit [1]/ AttributeList / NetworkSource / FlgNet / Parts / Access[@UId = '" + j + "'] / Symbol / Component");
modMethodXElement.SetAttributeValue("Name", AccVariablen[i]);
}
}
xdoc.Save("c:/export/" + NAME + ".xml");
最后以NULL异常结束。 我该如何解决这个问题?