C# 表达式必须计算为节点集
我似乎面临着一个相当复杂的问题(至少对我来说是复杂的)。我们有一个c#程序,它基本上解析模式文件并输出XML。 但现在我的任务是修改这个程序,使它能够添加更多的元素 让我试着解释一下 正如您在下图中所看到的,我有一个模式文件(为了方便起见,只附加了其中的一部分) 突出显示的部分是我所关心的,即USPhone、分机、外国电话。所有三个模式元素都位于Choice模式元素下,而US Phone和Extension位于sequence元素下 目前,对于突出显示的部分,生成的xml如下(以黄色突出显示) 然而,我想让这个项目推出这样的东西 i、 e.添加新元素,它是USPhone和Extension的父节点,并将新的e父节点添加到外部电话,这类似于ForeignAddress,它下面有其他元素 当程序递归地遍历每个模式对象并输出xml时,在选择[XmlSchemaChoice]时,我创建了一个新的XMLSchemaElement(USPhoneNumberGrp),它应该充当USPhone和扩展元素的父节点。但是当我尝试使用访问这个新添加的父节点时,问题就出现了 XmlNode parentNode=prototype.SelectSingleNode(路径) [prototype是一个XMLDocument,到目前为止,它是通过解析所有以前的模式元素创建的] 路径当前有一个值 path=“/PrototypeDocument/CAForm592B/stoppingagent/USPhoneNumberGrp” 但是我得到以下错误“表达式必须计算到节点集” 基本上这意味着路径本身是错误的,尽管当我查看到目前为止生成的XMLDocument(见下文)时,我确实看到了新添加的元素USPhoneNumberGrpC# 表达式必须计算为节点集,c#,xml,xpath,C#,Xml,Xpath,我似乎面临着一个相当复杂的问题(至少对我来说是复杂的)。我们有一个c#程序,它基本上解析模式文件并输出XML。 但现在我的任务是修改这个程序,使它能够添加更多的元素 让我试着解释一下 正如您在下图中所看到的,我有一个模式文件(为了方便起见,只附加了其中的一部分) 突出显示的部分是我所关心的,即USPhone、分机、外国电话。所有三个模式元素都位于Choice模式元素下,而US Phone和Extension位于sequence元素下 目前,对于突出显示的部分,生成的xml如下(以黄色突出显示)
<PrototypeDocument>
<CAForm592B LineNumber="" Description="""" Type="CAForm592BType" Documentation=""CA Form 592-B - Resident and Nonresident Withholding Tax Statement"" ValidValuesForFields="" MaxLengthOfFields="" documentName="CAForm592B">
<Amended MinOccurs="0" LineNumber="Top of Form" Description=""Amended"" Type="BooleanType" Documentation=""Base type for a boolean. Typically used on an Yes or No field."" ValidValuesForFields="" MaxLengthOfFields="" />
<WithholdingAgent LineNumber="Part I" Description=""Withholding agent"" Type="WithholdingAgent" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<Name LineNumber="" Description=""Withholding agent's (payer's/S Corporation/Partnership's/LLC/Trust) name"" Type="CA-BusinessNameType" Documentation=""Type for a Business Name according to CA specifications. Legal Characters: A-Z, a-z, 0-9, hash, hyphen, parentheses, ampersand, apostrophe and single space. Illegal Character: leading space, trailing space, adjacent spaces, and other symbols."" ValidValuesForFields="" MaxLengthOfFields="70" />
<IDNumber LineNumber="" Description=""Identification Number"" Type="IDNumber" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<Individual ChoiceID="b7eaf17b-d515-48b4-b24f-0eb3e136273d" LineNumber="" Description=""Individual identification number"" Type="Individual" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<SSN ChoiceID="746e28ef-3a82-4de6-b7d1-09a336474669" LineNumber="" Description=""Social Security Number"" Type="SSNType" Documentation=""Type for Social Security No. - 9 digits"" ValidValuesForFields="" MaxLengthOfFields="" />
<ITIN ChoiceID="746e28ef-3a82-4de6-b7d1-09a336474669" LineNumber="" Description=""IRS Individual Taxpayer Identification Number"" Type="CA-ITINType" Documentation=""CA type for Federal ITIN"" ValidValuesForFields="" MaxLengthOfFields="" />
<NotApplicable ChoiceID="746e28ef-3a82-4de6-b7d1-09a336474669" LineNumber="" Description=""Not applicable. ID number is individual in type and not applicable per a qualifying reason"" Type="CA-NotApplicableType" Documentation=""CA type for entity ID information to indicate that no Foreign entity Id or CA State Id number applies or exists."" Enum1="Foreign Non-US" Enum2="Applied For" ValidValuesForFields="Foreign Non-US;Applied For" MaxLengthOfFields="" />
</Individual>
<Other ChoiceID="b7eaf17b-d515-48b4-b24f-0eb3e136273d" LineNumber="" Description=""Other business or non-individual identification number"" Type="CA-BusinessIdType" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<CACorporationNumber ChoiceID="87b268af-3e7a-4fd2-9cff-c2547f34ec17" LineNumber="" Description=""California corporation number"" Type="CA-CorporationNumberType" Documentation=""Type for California corporation number"" ValidValuesForFields="" MaxLengthOfFields="" />
<SOSNumber ChoiceID="87b268af-3e7a-4fd2-9cff-c2547f34ec17" LineNumber="" Description=""California Secretary of State number"" Type="CA-SOSNType" Documentation=""Type for California Secretary of State file number"" ValidValuesForFields="" MaxLengthOfFields="" />
<LLCTempNumber ChoiceID="87b268af-3e7a-4fd2-9cff-c2547f34ec17" LineNumber="" Description=""Limited Liability Company temporary number"" Type="CA-LLCTempNumberType" Documentation=""CA type for LLC Temporary Number assigned by FTB"" ValidValuesForFields="" MaxLengthOfFields="" />
<NotApplicable ChoiceID="87b268af-3e7a-4fd2-9cff-c2547f34ec17" LineNumber="" Description=""Not Applicable per qualifying reason"" Type="CA-NotApplicableType" Documentation=""CA type for entity ID information to indicate that no Foreign entity Id or CA State Id number applies or exists."" Enum1="Foreign Non-US" Enum2="Applied For" ValidValuesForFields="Foreign Non-US;Applied For" MaxLengthOfFields="" />
<FEIN ChoiceID="87b268af-3e7a-4fd2-9cff-c2547f34ec17" LineNumber="" Description=""Federal employee identification number"" Type="EINType" Documentation=""Type for Employer Identification No. - 9 digits"" ValidValuesForFields="" MaxLengthOfFields="" />
</Other>
</IDNumber>
<USAddress ChoiceID="f97d374d-0961-4101-bdd0-85099615eb54" LineNumber="" Description=""United States Address"" Type="USAddressType" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<AddressLine1Txt LineNumber="" Description="""" Type="StreetAddressType" Documentation=""Address line 1"" ValidValuesForFields="" MaxLengthOfFields="35" />
<AddressLine2Txt MinOccurs="0" LineNumber="" Description="""" Type="StreetAddressType" Documentation=""Address line 2"" ValidValuesForFields="" MaxLengthOfFields="35" />
<CityNm LineNumber="" Description="""" Type="CityType" Documentation=""City"" ValidValuesForFields="" MaxLengthOfFields="22" />
<StateAbbreviationCd LineNumber="" Description="""" Type="StateType" Documentation=""State"" Enum1="AL" Enum2="AK" Enum3="AS" Enum4="AZ" Enum5="AR" Enum6="CA" Enum7="CO" Enum8="MP" Enum9="CT" Enum10="DE" Enum11="DC" Enum12="FM" Enum13="FL" Enum14="GA" Enum15="GU" Enum16="HI" Enum17="ID" Enum18="IL" Enum19="IN" Enum20="IA" Enum21="KS" Enum22="KY" Enum23="LA" Enum24="ME" Enum25="MH" Enum26="MD" Enum27="MA" Enum28="MI" Enum29="MN" Enum30="MS" Enum31="MO" Enum32="MT" Enum33="NE" Enum34="NV" Enum35="NH" Enum36="NJ" Enum37="NM" Enum38="NY" Enum39="NC" Enum40="ND" Enum41="OH" Enum42="OK" Enum43="OR" Enum44="PW" Enum45="PA" Enum46="PR" Enum47="RI" Enum48="SC" Enum49="SD" Enum50="TN" Enum51="TX" Enum52="VI" Enum53="UT" Enum54="VT" Enum55="VA" Enum56="WA" Enum57="WV" Enum58="WI" Enum59="WY" Enum60="AA" Enum61="AE" Enum62="AP" ValidValuesForFields="See instructions for valid values" MaxLengthOfFields="" />
<ZIPCd LineNumber="" Description="""" Type="ZIPCodeType" Documentation=""ZIP code"" ValidValuesForFields="" MaxLengthOfFields="" />
</USAddress>
<ForeignAddress ChoiceID="f97d374d-0961-4101-bdd0-85099615eb54" LineNumber="" Description=""Foreign Address"" Type="CA-ForeignAddressType" Documentation="" ValidValuesForFields="" MaxLengthOfFields="">
<StreetAddress LineNumber="" Description=""Address line 1"" Type="CA-StreetAddressType" Documentation=""Type for a Street Address according to CA specifications"" ValidValuesForFields="" MaxLengthOfFields="" /><InCareOfAddress MinOccurs="0" LineNumber="" Description=""Address line 2"" Type="CA-StreetAddressType" Documentation=""Type for a Street Address according to CA specifications"" ValidValuesForFields="" MaxLengthOfFields="" />
<City MinOccurs="0" LineNumber="" Description=""City"" Type="TextType" Documentation=""Used for a text field. Legal Characters: printable characters from ! to ~ plus symbols from ¡ to ¿ plus Spanish characters and single space. Illegal Character: leading space, trailing space, adjacent spaces."" ValidValuesForFields="" MaxLengthOfFields="50" />
<Country LineNumber="" Description=""Country"" Type="CA-CountryType" Documentation=""CA type for foreign address country which may include province and/or state"" ValidValuesForFields="" MaxLengthOfFields="19" />
</ForeignAddress>
<USPhoneNumberGrp ChoiceID="2d3ce0ab-bc99-465f-a80c-b70dd0c9ebff" LineNumber="" Description="""" Type="" Documentation="" ValidValuesForFields="" MaxLengthOfFields="" />
</WithholdingAgent>
</CAForm592B>
</PrototypeDocument>
如果我用XmlDocument.LoadXml()
加载您问题中的XML,然后用您提供的路径调用SelectSingleNode(path)
——效果很好,没有例外。您能创建一个复制问题的方法吗?在XSLT中,这种工作要容易得多。。。