我想知道是否可以在linq语句中包含内部变量或委托?
我目前正在使用带有XPath扩展的Linq到XML,并且在我无法保证的元素上使用where子句.
这是我的意思的样本:
var result =
from record in xml.Root.XPathSelectElements("record")
where ...
select record;
Run Code Online (Sandbox Code Playgroud)
我想要在哪里:
where
{
var element = record.XPathSelectElement("element[@type='sometype']");
return (element != null && element.Value.Contains("keyword"));
}
Run Code Online (Sandbox Code Playgroud) 使用LINQ to XML,如何基于序数位置连接两组数据?
<document>
<set1>
<value>A</value>
<value>B</value>
<value>C</value>
</set1>
<set2>
<value>1</value>
<value>2</value>
<value>3</value>
</set2>
</document>
Run Code Online (Sandbox Code Playgroud)
根据上面的片段,我想加入两组,使"A"和"1"在同一记录中,"B"和"2"在同一记录中,"C"和"3" "在同一记录中.
我正在尝试检查XML响应中是否存在用户.
当用户不存在时,响应如下:
<ipb></ipb>
Run Code Online (Sandbox Code Playgroud)
对于我(在代码中)验证用户不存在的最佳方法是什么?我在考虑检查它是否没有任何子元素,但我有点困惑.
谢谢您的帮助!
public void LoadUserById(string userID)
{
doc = XDocument.Load(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userID));
if (doc.DescendantNodes().ToList().Count < 1)
{
userExists = false;
}
else
{
userExists = true;
}
}
Run Code Online (Sandbox Code Playgroud) 因此,我可以轻松地使用LINQ to XML来遍历正确设置的XML文档.但是我在解决如何将其应用于HTML表时遇到了一些问题.这是设置:
<table class='inner'
width='100%'>
<tr>
<th>Area</th>
<th>Date</th>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Zip Code</th>
<th>Type</th>
<th>Amount</th>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
基本上,可以有无穷无尽的行数,我希望能够逐行检查数据.谁能指出我正确的方向?我应该使用LINQ以外的工具吗?
编辑:对于混淆感到抱歉,我的问题是我尝试从中收集数据的页面是HTML,而不是XML.确切的扩展名是".aspx.htm".这似乎没有正确加载,即使它确实如此,我不确定如何遍历HTML页面,因为在表之前有一个表我正在尝试从中获取数据.
例如,这是表格中的XPATH,我试图从中获取信息:
/html/body/form/div[3]/table/tbody/tr[5]/td/table
Run Code Online (Sandbox Code Playgroud) 这是我想要做的:
string parseCode = from x in xml.Descendants("LogType")
where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
select x.Attribute("ParseCode").Value;
Run Code Online (Sandbox Code Playgroud)
但是这会产生错误:"无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'string'"
只有一个值,x.Attribute("ParseCode")但它坚持返回类型IEnumerable<string>.如何将该值提取到字符串中?
编辑:谢谢你的回复.这对我有用:
string parseCode = (from x in xml.Descendants("LogType")
where x.Attribute("ID").Value == (string) ddlHistoryLogDefinitions.SelectedValue
select (string) x.Attribute("ParseCode").Value).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
这个技巧是在.FirstOrDefault()之前将整个linq查询包装在()中.
我无法从我的XML中获取元素.我无法从我的XML获取Vehicles或Vehicle元素,它总是返回null.
谁能看到我出错的地方?
这是我的代码......
[TestMethod]
public void TestDeleteVehicleFromXMLFile()
{
using (FileStream stream = new FileStream(base._TestXPathXMLFile, FileMode.Open))
{
try
{
XDocument xDoc = XDocument.Load(stream);
var q = from RootNode in xDoc.Descendants("VehicleCache")
select new
{
// Vehicles & VehiclesList is always null
Vehicles = RootNode.Elements(XName.Get("Vehicle")).ToList(),
VehiclesList = RootNode.Elements(XName.Get("Vehicles")).ToList(),
SelfNode = RootNode.DescendantNodesAndSelf().ToList(),
DescendantNodes = RootNode.DescendantNodes().ToList()
};
// used to see what is in item
foreach (var item in q)
{
int i = 0;
}
}
catch(Exception E)
{
Assert.Fail(E.Message);
}
}
}
<VehicleCache> …Run Code Online (Sandbox Code Playgroud) 这是xml内容.
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>1</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>2</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r><w:t>3</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove -->
</w:tc>
<w:tc>
<w:p>
<w:r><w:t>4</w:t></w:r>
</w:p>
<w:p /> <!-- needs to remove -->
<w:p /> <!-- needs to remove …Run Code Online (Sandbox Code Playgroud) 我想要一个将返回IEnumerable字符串的内部查询
'as.m3''as.m4'
我曾尝试 xDoc.Elements("moduleid")和xDoc.Descendents("moduleid")
没有运气
<?xml version="1.0" encoding="UTF-8">
<root>
<code>M11088MUBWWLSRSV9LTJBH81QT</code>
<moduleid>as.m3</moduleid>
<moduleid>as.m4</moduleid>
</root>
Run Code Online (Sandbox Code Playgroud) 我一直在使用LINQ大约2天,所以请耐心等待.
这是我目前的代码;
_resultList = (
from
item in _documentRoot.Descendants("MyItems")
select
new MyClass
{
XMLID = item.Attribute("id").Value
}).ToList<MyClass>();
Run Code Online (Sandbox Code Playgroud)
大多数元素都有'id'属性,并且它们会成功添加到列表中.但是,有些人没有'id'.对于这些,我希望'id'只是一个空字符串.
在尝试访问该属性之前,如何检查该属性是否存在?谢谢
我有一个字符串,该字符串以xml元素开头,然后在元素结束后以常规文本继续。
像这样:
<SomeElement SomeAtt="SomeValue"><SomeChild/></SomeElement> More random text.
Run Code Online (Sandbox Code Playgroud)
我想将第一部分解析为XElement,然后将以下文本分离为字符串变量。我曾考虑过仅计算尖括号,但是有合法的XML会让我失望。我更喜欢使用开箱即用的解析器。我尝试使用XmlReader和XElement.Parse方法。我希望它们在元素读取后停止,而不是因为Xml元素后出现意外文本而引发异常。到目前为止,我还没做到。XmlReader有一个ReadSubtree方法,但是我无法使它工作。
有任何想法吗?
编辑