我需要为当前的xml提供包装器,从中获取keyvalye对值。以下是我的当前代码:
string SID_Environment = "SID_" + EnvironmentID.ToString();
XDocument XDoc = XDocument.Load(FilePath_EXPRESS_API_SearchCriteria);
var Dict_SearchIDs = XDoc.Elements().ToDictionary(a => (string)a.Attribute("Name"), a => (string)a.Attribute("Value"));
string Search_ID = Dict_SearchIDs.Where(IDAttribute => IDAttribute.Key == SID_Environment).Select(IDAttribute => IDAttribute.Value).FirstOrDefault();
Console.WriteLine(Search_ID);下面是示例xml,如下所示:
<APIParameters>
<Parameter Name="SID_STAGE" Value="101198" Required="true"/>
<Parameter Name="SID_QE" Value="95732" Required="true"/>
</APIParameters>请注意,这段代码在示例xml中运行得很好,但是在用一些包装器修改了我的xml之后,我将面临这个问题。我需要为我的xml提供一些包装器来修改我的示例xml,如下所示:
<DrWatson>
<Sets>
<Set>
<APIParameters>
<Parameter Name="SID_STAGE" Value="101198" Required="true"/>
<Parameter Name="SID_QE" Value="95732" Required="true"/>
</APIParameters>
</Set>
</Sets>
</DrWatson>但是当我这样做并运行我的代码时,它会抛出一个error.Please建议。
发布于 2013-09-18 09:42:35
你需要这样的东西:
var apiParams = doc.Descendants("APIParameters");然后您可以修改代码:
var Dict_SearchIDs = apiParams.Elements().ToDictionary(a => (string)a.Attribute("Name"), a => (string)a.Attribute("Value"));发布于 2013-09-18 09:35:23
XDoc.Elements()只返回直接子元素,而是使用Descendants。
var parameterElements = xDoc.Descendants("Parameter");
parameterElements.ToDictionary(a => (string)a.Attribute("Name"),
a => (string)a.Attribute("Value"));https://stackoverflow.com/questions/18868504
复制相似问题