我有简单的XML,我想查询助记符集合.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<logs version="1.3.1.1" xmlns="http://www.witsml.org/schemas/131">
<log>
<startIndex uom="m">200.29</startIndex>
<endIndex uom="m">209.73</endIndex>
<logCurveInfo>
<mnemonic>DEPTH</mnemonic>
</logCurveInfo><logCurveInfo>
<mnemonic>VDEPTH</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>ropAv1</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>wobAv1</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>hkldAv1</mnemonic>
</logCurveInfo>
<logData>
<data />
</logData>
</log>
</logs>
Run Code Online (Sandbox Code Playgroud)
我试过了,
XDocument xDoc = XDocument.Load(@"e:\sampleXml.xml");
var q = from c in xDoc.Descendants("logCurveInfo")
select c.Element("mnemonic").Value;
foreach (string item in q)
{
MessageBox.Show(item);
}
Run Code Online (Sandbox Code Playgroud)
虽然查询正在执行,但我没有得到任何输出.我希望每个助记符都显示在循环中的消息框中.
我写了以下代码
List<Pupils> pupils = PupilsDAO.SelectDAO();
XElement dtpupil = new XElement("DtDatas",
from xlist in pupils
orderby xlist.Id
select new XElement("DtData",
new XElement("ref", xlist.Id),
new XElement("forename", xlist.Forename),
new XElement("surname", xlist.Surname)
)
);
Run Code Online (Sandbox Code Playgroud)
我没有为列表中的每个元素获取不同的XML对象,而是为列表中的每个项目获取输出,但它们都是相同的而不是实际迭代,所以只需加载...
<DtDatas>
<DtData>
<ref>01</ref>
<forename>joe</forename>
<surname>bloggs</surname>
</DtData>
<DtData>
<ref>01</ref>
<forename>joe</forename>
<surname>bloggs</surname>
</DtData>
<DtData>
<ref>01</ref>
<forename>joe</forename>
<surname>bloggs</surname>
</DtData>
<DtData>
<ref>01</ref>
<forename>joe</forename>
<surname>bloggs</surname>
</DtData>
</DtDatas>
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法?我是不是要为列表添加迭代?
使用XDocument与xpath解析XML哪一个是性能比较好?
例如,在xml中搜索标签并获取值
tags = xmlDoc.Descendants(xmlTag);
Run Code Online (Sandbox Code Playgroud)
要么
xml.SelectSingleNode("//root/node")
Run Code Online (Sandbox Code Playgroud)
那么哪一个会更快?
我正在尝试编写一个正则表达式来匹配一个字符串形式:
"[A-Za-z][A-Za-z]-[A-Za-z][A-Za-z]_[match all chars]"
Run Code Online (Sandbox Code Playgroud)
我想要匹配的字符串必须是这种形式,包括连字符和下划线.到目前为止,我有:
Regex regEx = new Regex(@"[A-Za-z]+(-[A-Za-z]+)+*$", RegexOptions.IgnorePatternWhitespace);
Run Code Online (Sandbox Code Playgroud)
我不确定如何添加下划线字符,以便匹配.
这个元素是我希望匹配的XML元素; 我还想检索这个元素的内容.我怎么能这样做?
var newVar = from e in doc.Descendants("DocumentElement").Descendants()
where regEx.IsMatch(e.Name.LocalName)
select e;
Run Code Online (Sandbox Code Playgroud) 我试图得到一个独特的元素列表,但它总是返回一切.
XML:
<root>
<row>
<unit>CAN</unit>
</row>
<row>
<unit>KG</unit>
</row>
<row>
<unit>KG</unit>
</row>
<row>
<unit>PKT</unit>
</row>
<row>
<unit>CAN</unit>
</row>
<row>
<unit>PKT</unit>
</row>
<row>
<unit>KG</unit>
</row>
</root>
Run Code Online (Sandbox Code Playgroud)
LINQ:
List<XElement> elements = (from e in xdoc.Descendants("row").Elements()
where e.Name.Equals("unit")
select e).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
预期输出:元素列表应包含3个项目
<unit>CAN</unit>
<unit>KG</unit>
<unit>PKT</unit>
Run Code Online (Sandbox Code Playgroud) 我有这个包含实体列表的XML:
<?xml version="1.0" encoding="utf-8" ?>
<Connections>
<Connection>
<ConnectionName>connName</ConnectionName>
<InterfaceName>Account Lookup</InterfaceName>
<RequestFolder>C:\Documents and Settings\user\Desktop\Requests</RequestFolder>
<ResponseFolder>C:\Documents and Settings\user\Desktop\Responses</ResponseFolder>
</Connection>
</Connections>
Run Code Online (Sandbox Code Playgroud)
我正在尝试根据其名称检索其中一个并从中构建一个对象.
var results = (from i in this.Elements("Connection")
where i.Element("ConnectionName").ToString() == stubConnectionName
select new {
interfaceName = ((string)i.Element("InterfaceName").Value),
requestFolder = ((string)i.Element("RequestFolder").Value),
responseFolder = ((string)i.Element("ResponseFolder").Value),
}).Single();
return new StubConnection(stubConnectionName, results.interfaceName, results.requestFolder, results.responseFolder);
Run Code Online (Sandbox Code Playgroud)
问题是结果是空的.我的查询有什么问题?
我目前正在使用WPF应用程序.现在,我想将我的数据保存到XML文件中.如果在项目中找不到xml文件,则创建一个新文件.有谁能教我怎么样?
我认为代码将是这样的
public MainWindow()
{
InitializeComponent();
loadXML();
}
public void loadXML()
{
xDocument doc = xDocument.load("MyXmlFile.xml");
if(doc.exist== false)
{
//create new xml
}
}
Run Code Online (Sandbox Code Playgroud) 我必须将整个XML文档传递给第三方函数.参数是XmlElement.
要做到这一点,我已经成功地使用了这个:
XmlDocument doc;
//doc = ...
XmlElement root = doc.DocumentElement;
3rdPartyFunction(root);
Run Code Online (Sandbox Code Playgroud)
但现在我用XDocument而不是XmlDocument:
XDocument doc;
//doc = ...
//how to call 3rdPartyFunction?
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何调用该函数?我可以从"Xml"转换为"X"吗?
我有一个ASP.NET MVC网站。该网站的站点地图如下所示:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.mysite.com/contact</loc>
<lastmod>2013-06-04</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/contact-us</loc>
<lastmod>2013-06-04</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/books</loc>
<lastmod>2013-06-18</lastmod>
<changefreq>monthly</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog</loc>
<lastmod>2012-05-02</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog/post-1</loc>
<lastmod>2012-05-02</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog/post-2</loc>
<lastmod>2012-02-15</lastmod>
<changefreq>never</changefreq>
</url>
</urlset>
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何使用C#中的Linq-to-XML查询此站点地图。我正在尝试编写一个仅返回博客文章条目的查询。博客文章条目的loc属性值以http://www.mysite.com/about/blog/开头。目前,我正在成功加载和查询站点地图。但是,我无法弄清楚如何仅过滤博客文章,然后按lastmod值排序。这是我到目前为止的内容:
XDocument sitemap = XDocument.Load(Server.MapPath("/resources/sitemap.xml"));
IEnumerable<XElement> blogs = from post in sitemap.Descendants("url")
select post;
Run Code Online (Sandbox Code Playgroud)
如何只过滤我的博客文章?我什至只查询网址似乎都行不通。
我只想知道如何使用XDocument对整个元素进行注释.
XDocument doc = XDocument.Parse("<configuration>
<connectionString>
...
</connectionString>
<configuration>");
/*Something like that*/ doc.Root.Element("connectionStrings").NodeType = XComment; /*??*/
Run Code Online (Sandbox Code Playgroud)