我有一个像这样的xml结构:
<Items>
<Configuration>
<ConfigurationSetting>Setting1</ConfigurationSetting>
<ConfigurationSetting>Setting2</ConfigurationSetting>
</Configuration>
<MetaData>
...
</MetaData>
<Group>
<GroupType>MyType1</GroupType>
<GroupType>MyType2</GroupType>
</Group>
<Group> <--- Looking for this Element
<Reference>MyReference1</Reference>
<Reference>MyReference2</Reference>
</Group>
<Group>
<GroupType>MyType3</GroupType>
<GroupType>MyType4</GroupType>
</Group>
</Items>
Run Code Online (Sandbox Code Playgroud)
我想写一个Linq查询,返回包含子元素"引用"的第一个"组"元素.
谢谢你的帮助.
这是我将得到的回应:
<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
<image_hash>cxmHM</image_hash>
<delete_hash>NNy6VNpiAA</delete_hash>
<original_image>http://imgur.com/cxmHM.png</original_image>
<large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
<small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
<imgur_page>http://imgur.com/cxmHM</imgur_page>
<delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>
Run Code Online (Sandbox Code Playgroud)
如何提取每个标签的值?
XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));
string originalImage = 'do the extraction here';
string imgurPage = 'the same';
UploadedImage image = new UploadedImage();
Run Code Online (Sandbox Code Playgroud) 我是Linq的新手请指导我一些基本的事情.
在阅读Linq的一些文章.一些authers从Linq查询中填充var中的数据,一些填充自定义类型对象的列表,一些填充IEnumerable中的数据,一些填充IQuryable中的数据.我无法得到这些4中的不同之处以及应该在哪种情况下使用哪一个.
我想使用Linq to SQL.我该怎么用?
linq linq-to-objects linq-to-entities linq-to-xml linq-to-sql
如何从此Feed中获取"入口"节点
http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348
我试过linq到xml,但我认为因为代码后面的条目标签的现有属性不起作用.
string url = "http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348";
WebClient c = new WebClient();
string xml = ASCIIEncoding.Default.GetString(c.DownloadData(url));
XDocument doc = XDocument.Parse(xml);
var entries = doc.Descendants("entry");
Run Code Online (Sandbox Code Playgroud)
提前致谢,
我是一个LINQ to XML的新手,我有这个代码可以工作(大部分时间):
private long processFile(StreamWriter oWriter, string inFileName)
{
XDocument xmlDoc = XDocument.Load(inFileName);
List<DocMetaData> docList =
(from d in xmlDoc.Descendants("DOCUMENT")
select new DocMetaData
{
Folder = d.Element("FOLDER").Attribute("name").Value
,
File = d.Element("FILE").Attribute("filename").Value
,
Comment = d.Elements("INDEX")
.Where(i => i.Attribute("name").Value == "Comment(idmComment)")
.First()
.Attribute("value").Value
,
Title = d.Elements("INDEX")
.Where(i => i.Attribute("name").Value == "Title(idmName)")
.First()
.Attribute("value").Value
,
DocClass = d.Elements("INDEX")
.Where(i => i.Attribute("name").Value == "Document Class(idmDocType)")
.First()
.Attribute("value").Value
}
).ToList<DocMetaData>();
OutputListToFile(oWriter, docList);
return docList.LongCount();
}
Run Code Online (Sandbox Code Playgroud)
这在第117行(选择表达式)上失败:
System.NullReferenceException: Object reference not set to …Run Code Online (Sandbox Code Playgroud) 我想用Linq读取一个xml文件.这个xml文件由1个头和N个子元素组成,如下所示:
<rootNode>
<header>
<company name="Dexter Corp." />
</header>
<elements>
<element>one</element>
<element>eleven</element>
<element>three</element>
<element>four</element>
<element>five</element>
<element>three</element>
<element>two</element>
<element>two</element>
</elements>
</rootNode>
Run Code Online (Sandbox Code Playgroud)
我想检索一个值的元素(例如:2).只有当我检索元素时,才会获得标题元素.
今天,我喜欢这样:
string xmlFilePath = @"C:\numbers.xml";
XDocument doc = XDocument.Load(xmlFilePath);
var header = doc.Descendants().Elements("header");
var elements = doc.Descendants()
.Elements("elements")
.Elements("element")
.Where(el => el.Value == "two");
// I get this values even if there is no 'elements'
string companyName = header.Descendants("company").Attributes("name").Single().Value;
string serialNumber = header.Descendants("serial").Single().Value;
return elements.Select(el => new {Company = companyName, Serial = serialNumber, Value = el.Value});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来解析文件?(并提高性能?)
我试图解析一个在那里有日期的xml字符串.我想要填充的对象具有可为空的DateTime.但是如果我拉回的字符串有一个空值,我希望它是最小日期值.我想将它分配给变量?使用LINQ有一种简单的方法吗?
IEnumerable<PatientClass> template = (IEnumerable<PatientClass>)(from templates in xDocument.Descendants("dataTemplateSpecification")//elem.XPathSelectElements(string.Format("//templates/template[./elements/element[@name=\"PopulationPatientID\"and @value='{0}' and @enc='{1}']]", "1", 0))
select new PatientClass
{
PCPAppointmentDateTime = DateTime.Parse(templates.Descendants("element").SingleOrDefault(el => el.Attribute("name").Value == "PCPAppointmentDateTime").Attribute("value").Value),
});
Run Code Online (Sandbox Code Playgroud)
我正在使用的对象是......
class PatientClass
{
public DateTime? PCPAppointmentDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有以下xml:
<root ...>
<Tables>
<Table content="..">
</Table>
<Table content="interesting">
<Item ...></Item>
<Item ...></Item>
<Item ...></Item>
</Table>
...etc...
</Tables>
</root>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码从"有趣"节点获取项目:
XElement xel = XElement.Parse(resp);
var nodes = from n in xel.Elements("Tables").Elements("Table")
where n.Attribute("content").Value == "interesting"
select n;
var items = from i in nodes.Elements()
select i;
Run Code Online (Sandbox Code Playgroud)
是否有更简单,更清洁的方法来实现这一目标?
我试图读取XML文件,但type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'由于以下查询而收到错误:
List<Data> dogs = (from q in doc.Descendants("dog")
where (string)q.Attribute("name") == dogName
select new Data
{
name = q.Attribute("name").Value,
breed = q.Element("breed").Value,
sex = q.Element("sex").Value
}.ToList<Data>);
Run Code Online (Sandbox Code Playgroud)
数据类:
public class Data
{
public string name { get; set; }
public string breed { get; set; }
public string sex { get; set; }
public List<string> dogs { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 运行这行代码时,我收到一个System.NullReferenceException.
// Occurs on this line
if (xDoc.Root.Element("data").Element("forecast").Element("record").Value == "0")
{
tempnow.Text = xDoc.Root.Element("data").Element("forecast").Element("pop").Value;
}
Run Code Online (Sandbox Code Playgroud)
Xdoc定义在类下面,如果这与它有关.
{
public partial class MainPage : PhoneApplicationPage
{
XDocument xDoc;
Run Code Online (Sandbox Code Playgroud)
这是我试图访问的XML文档,并找到"记录"的值.这也是"Xdoc"所代表的文档,其中包含以下信息.
<data>
<status>success</status>
<location>
<city/>
<fullName>39.6N, 0.3W - 6miles SW of El Puerto, SP</fullName>
<localDate>2014-01-30T01:29:23</localDate>
<localEpoch>1391045363</localEpoch>
<locationID>GP202368</locationID>
</location>
<forecast>
<record>0</record>
<utcEpoch>1391040000</utcEpoch>
<utcDate>2014-01-30T00:00:00</utcDate>
<maxTempC>15.4</maxTempC>
<maxTempF>59.7</maxTempF>
<minTempC>5.2</minTempC>
<minTempF>41.3</minTempF>
<fcstDay>1</fcstDay>
<dayLength>10:07</dayLength>
<sunriseHHMMLocal>8:11am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:18pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>19</pop>
<uv_index>2</uv_index>
<gustC>30</gustC>
<gustF>19</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>partly-cloudy-sm.png</icon>
<iconBase>partly-cloudy</iconBase>
<iconLg>partly-cloudy.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>33</rh>
<skyCover>8</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>NW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF> …Run Code Online (Sandbox Code Playgroud) linq-to-xml ×10
c# ×9
linq ×3
.net ×2
xml ×2
feedparser ×1
generics ×1
linq-to-sql ×1
performance ×1