对于以下代码,Linq to XML的等价物是什么:
public List<listing> GetList()
{
List<listing> listings = new List<listing>();
if(File.Exists(this.XmlFilePath))
{
DataSet ds = new DataSet();
ds.ReadXml(this.XmlFilePath);
DataTable dt = ds.Tables["listing"];
for(int row = 0; row < dt.Rows.Count; row++)
{
listing listing = new listing();
listing.A = dt.Rows[row]["A"].ToString();
listing.B = dt.Rows[row]["B"].ToString();
listing.C = dt.Rows[row]["C"].ToString();
listing.D= dt.Rows[row]["D"].ToString();
listing.E = dt.Rows[row]["E"].ToString();
listings.Add(listing);
}
}
return listings;
}
Run Code Online (Sandbox Code Playgroud) 这是一个新手问题,所以对我很好:)
如何在ASP.NET中使用php API?此API返回XML文档.它还能够返回JSON.
输出如下所示
XML
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Country>US</Country>
<City>Seattle</City>
<Result>Done</Result>
</Address>
Run Code Online (Sandbox Code Playgroud)
JSON
{
"CountryCode" : "US",
"City" : "Seattle",
"Result" : "Done"
}
Run Code Online (Sandbox Code Playgroud)
例如:有一个服务http://someservice.com/name_query.php?pincode= ,它接受pincode并返回一个XML文档.
我可以使用LINQtoXML并使用它.请使用XML和使用JSON的示例非常有用.
我正在构建一个xml文件.该文件的一部分是静态的.一些文件是动态的.我的代码有一个"空对象引用"错误.
任何提示都会很棒.
private XElement BuildDataElement()
{
// this is going to be more complicated
return new XElement("data");
}
public void TestXML(string fname)
{
// build the data element
XElement allData = BuildDataElement();
// Build the header
XDocument doc = new XDocument(
new XElement("map",
new XAttribute("showLabels", "1"),
new XAttribute("includeNameInLabels", "1"),
new XElement("colorRange",
new XElement("color",
new XAttribute("minValue", "1")
)
),
allData,
new XElement("application",
new XElement("apply",
new XAttribute("toObject", "TOOLTIP"),
new XAttribute("styles", "TTipFont,MyDataPlotStyle")
)
)
)
);
if (File.Exists(fname))
File.Delete(fname);
doc.Save(fname);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用xlinq进行一些变换,其中一些变换可能导致在文档中留下空元素.
完成所有这些转换后,如何查询xdocument中的所有空元素?
换一种说法; 如果我删除了<a>碰巧是<li>标签内唯一元素的所有标签,我该如何删除空标签<li>?
之前:
XDocument.Parse(@"<body>
<ul><li><a href="#">Joy</a></li></ul>
<p>Hi.</p>
</body>").Descendants("a").Remove();
Run Code Online (Sandbox Code Playgroud)
后:
<body>
<ul><li/></ul>
<p>Hi.</p>
</body>
Run Code Online (Sandbox Code Playgroud)
我会比较喜欢:
<body>
<p>Hi.</p>
</body>
Run Code Online (Sandbox Code Playgroud) 下面的代码有效,但我想使用yield或更改算法来优化代码.
public IEnumerable<Book> GetAuthorWithBookName(string keyword)
{
var bookAndAuthorList = new List<Book>();
List<Author> AuthorNameList = getAuthorName(keyword);
foreach (var author in AuthorNameList)
{
XmlNode booksNames = getBook(author);
XDocument XDOCbooksNames = XDocument.Parse(booksNames.OuterXml);
var bookNameList = (
from x1 in XDOCbooksNames.Descendants("Books")
select x1.Elements("book").Select(g => g.Attribute("name").Value))
.ToList();
foreach (var bookName in bookNameList)
{
bookAndAuthorList.Add(new Book()
{
authorName = author.authorName,
bookName = bookName
});
}
}
return bookAndAuthorList;
}
public class Book
{
public string authorName { get; set; }
public string bookName { get; set; …Run Code Online (Sandbox Code Playgroud) 我有一个app.config文件.
我想要检索和编辑属性:
<appSettings>
<add key="ProcessorInstances" value="2" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我正在使用此查询:
var list5 = from appNode in doc2.Descendants("appSettings").Elements()
where appNode.Attribute("key").Value == "ProcessorInstances"
select appNode;
var element5 = list5.FirstOrDefault();
string five = element5.Attribute("value").Value;
Run Code Online (Sandbox Code Playgroud)
但现在我面临的问题包括:
<app>
<file value="../../../logs/Intel.Service.log" />
</app>
Run Code Online (Sandbox Code Playgroud)
和:
<baseAddresses>
<add baseAddress="http://Machinename:portnumber/servicename/" />
</baseAddresses>
Run Code Online (Sandbox Code Playgroud)
这些元素没有任何键属性.
如何编写LINQ查询来编辑它们?
XDocument的内容是下面的XML.
我想获得一个List(),请参阅此消息的末尾.
<myXml>
<myDatas code="01">
<myVar name="myvar" value="A" />
<myData name="A" value="A1" />
<myData name="B" value="B1" />
</myDatas>
<myDatas code="02">
<myVar name="myvar" value="B" />
<myData name="A" value="A2" />
<myData name="D" value="D2" />
</myDatas>
</myXml>
public class MyData
{
public string MainCode { get; set; }
public string Code { get; set; }
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想要一个List()这个内容应该是这样的:
new MyData { MainCode = "01"; Code = "A"; Value = "A1" };
new MyData { MainCode = "01"; …Run Code Online (Sandbox Code Playgroud) 我正在用C#编写一个Windows窗体程序,我希望能够将信息保存到XML文件中.当我第一次创建XML文件时,我只想宣传声明
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Run Code Online (Sandbox Code Playgroud)
然后我想要的根节点称为"联系人".
最终文件应如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Contacts>
<Contact>
<Name>name</Name>
<Address>address</Address>
<Contact>
<Contacts>
Run Code Online (Sandbox Code Playgroud)
将有多个<Contact></Contact>元素.
我遇到的问题是我第一次创建XML文件.
我的XML操作在他们自己的类中.这是创建文件的方法:
public void createFile()
{
if (!File.Exists(fileName))
{
//Populate with data here if necessary, then save to make sure it exists
xmlFile = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("XML File for storing " + RootName));
xmlFile.Save(FileName);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到一个ArgumentNullException是未处理的错误.
任何想法如何实际获取文件中的数据并保存?谢谢
给出如下的站点地图:
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\">
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.kill.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.destroy.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.pillage.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
<sitemap><loc>http://www.whyisxdocumentsuchapain.com/sitemap.etc.xml</loc><lastmod>2013-12-10T18:34:09Z</lastmod></sitemap>
</sitemapindex>
Run Code Online (Sandbox Code Playgroud)
我如何使用XDocument linq查询获取loc节点内的四个URL ?
它似乎完全打败了我.
我必须创建一个编码为的xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
Run Code Online (Sandbox Code Playgroud)
目前我使用LINQ创建的xml标记为
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使用LINQ.
linq-to-xml ×10
c# ×8
xml ×6
linq ×5
.net ×1
app-config ×1
asp.net ×1
optimization ×1
sitemap ×1
winforms ×1
xelement ×1
xml-encoding ×1
yield ×1