我正在尝试在C#应用程序中使用LINQ从xml文件中提取信息,其中值大于数字参数。 xml使用不是URL的名称空间。我一直在查看类似的StackOverflow问题,而且似乎都有一个名称空间的url - 比如xmlns =" http://www.cgi.com/Ratabase" ;,但我看起来像xmlns: RS ="瓮:架构 - 微软-COM:行集"和xmlns:z ="#RowsetSchema"。
以下是xml的片段:
<configurations>
<system_config>
<section id="rs_centerinfo">
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly" rs:updatable="true">
<s:AttributeType name="CenterCode" rs:number="1" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="8" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="CenterName" rs:number="2" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="30" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="Version" rs:number="3" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="5" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="NextExport" rs:number="4" rs:write="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="0" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="NextImport" rs:number="5" rs:write="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="0" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="LastImportTime" rs:number="6" rs:write="true">
<s:datatype dt:type="dateTime" rs:dbtype="variantdate" dt:maxLength="16" rs:precision="0" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="FtpProxyServer" rs:number="7" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="40" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="FtpUserName" rs:number="8" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="20" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="FtpPassword" rs:number="9" rs:write="true">
<s:datatype dt:type="string" dt:maxLength="20" rs:precision="0" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="ResetImportID" rs:number="10" rs:write="true">
<s:datatype dt:type="boolean" dt:maxLength="2" rs:precision="0" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row CenterCode="JAX" CenterName="Jacksonville" Version="1.0" NextExport="66742" NextImport="29756" LastImportTime="2015-06-10T14:48:33" FtpProxyServer="" FtpUserName="" FtpPassword="" ResetImportID="False"/>
<z:row CenterCode="PHL" CenterName="Philadelphia" Version="1.0" NextExport="78993" NextImport="74506" LastImportTime="2015-06-10T14:48:35" FtpProxyServer="" FtpUserName="" FtpPassword="" ResetImportID="False"/>
<z:row CenterCode="AGS" CenterName="Augusta" Version="1.0" NextExport="82040" NextImport="9893" LastImportTime="2015-06-10T13:02:39" FtpProxyServer="" FtpUserName="" FtpPassword="" ResetImportID="False"/>
<z:row CenterCode="TYS" CenterName="Knoxville" Version="1.0" NextExport="74756" NextImport="15509" LastImportTime="2011-07-25T14:46:48" FtpProxyServer="" FtpUserName="" FtpPassword="" ResetImportID="False"/>
我尝试从z:row中选择属性,其中NextImport和NextExport大于50000进入列表,以便在网格中显示。
编辑:我不确定如何在LINQ中编码我的选择。我尝试使用以下代码回复:
XDocument xmldoc = XDocument.Load(@"c:\XML\Configuration.xml");
XNamespace rs = "urn:schemas-microsoft-com:rowset";
XNamespace z = "#RowsetSchema";
string test = xmldoc.Descendants(z + "row").Attributes("NextExport").ToString();
答案 0 :(得分:0)
您对命名空间的使用看起来很不错。因此,使用NextImport和NextExport&gt;查找所有{#RowsetSchema}row
行的查询50000看起来像
XNamespace z = "#RowsetSchema";
var limit = 50000L;
var elementQuery = xmldoc.Descendants(z + "row").Where(e => (long?)e.Attribute("NextExport") > limit && (long?)e.Attribute("NextImport") > limit);
((long?)
转换处理缺少属性的情况。)
我不完全确定你的意思是“我正在尝试从...中选择属性到列表中”。如果您想要一个元素列表本身,请执行:
var elements = elementQuery.ToList();
如果您只想在网格中使用NextImport
和NextExport
属性,则可以执行以下操作:
var attributePairs = elementQuery.Select(e => new[] { (string)e.Attribute("NextExport"), (string)e.Attribute("NextImport") }).ToList();
如果您想要网格中的所有属性,可以从以下内容开始:
var attributes = elementQuery.Select(e => e.Attributes().Select(a => new KeyValuePair<string, string>(a.Name.LocalName, (string)a)).ToList()).ToList();
请记住,XML标准将属性定义为unordered,因此无法保证属性在行与行之间的顺序相同。