我需要创建以下XML,我正在尝试使用XDocument.但是,我在指定名称空间时遇到了麻烦.
<AssessmentOrderRequest
xsi:schemaLocation="http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd"
xmlns="http://ns.hr-xml.org/2007-04-15"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</AssessmentOrderRequest>
Run Code Online (Sandbox Code Playgroud)
这是我正在寻找的那种代码,但是,我无法在名称中创建带冒号的属性xsi:schemaLocation.
return new XDocument(
new XElement("AssessmentOrderRequest",
new XAttribute("xsi:schemaLocation", XNamespace.Get("http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd")),
new XAttribute("xmlns", XNamespace.Get("http://ns.hr-xml.org/2007-04-15")),
new XAttribute(XNamespace.Xmlns + "xsi", XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"))
)
);
Run Code Online (Sandbox Code Playgroud) 我有这样的XML结果
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">16</int>
</lst>
<result name="response" numFound="3" start="0" maxScore="1.0">
<doc>
<str name="ContaFirstname">
firstname1
</str>
<str name="ContaId">6557</str>
<str name="ContaJobTitle">Manager</str>
<str name="ContaSurname">surname1
</str>
</doc>
<doc>
<str name="ContaFirstname">firstname2</str>
<str name="ContaId">6203</str>
<str name="ContaJobTitle">Director</str>
<str name="ContaSurname">surname2</str>
</doc>
</result>
</response>
Run Code Online (Sandbox Code Playgroud)
我想获得对象的列表,每个对象将包含的价值ContaFirstname,ContaId,ContaJobTitle和ContaSurname
我试过这样的东西,但这不对,因为我把它们都弄得一团糟
var test = from c in xml.Descendants("doc")
select new
{
firstname = c.Element("ContaFirstname"),
surnmane = c.Element("ContaSurname")
};
Run Code Online (Sandbox Code Playgroud)
那么如何通过名称访问这些元素呢?
.Net框架现在(至少)有四种不同的读取Xml字符串的方法.我已经使用了XmlDocument,XmlReader,XPath和XElement中的每一个,但在编码或执行期间使用哪个是最有效的?每个人都是为不同的任务而设计的,有什么优缺点?
更新: 使用XmlReader似乎是读取xml最快捷的方式,这听起来对我很合理,但有其局限性.我想知道XmlDocument和XLinq之间是否存在非连续访问xml的性能差异.
更新: 我发现一些帖子比较了加载xml文档的不同方法.XmlReader是最快的,在您加载具有10,000+节点的文档之前,XmlDocument和LINQ to XML之间存在微不足道的差异,其中LINQ to XML出现在前面.
Dim xml = <Root>
<Parent id="1">
<Child>Thomas</Child>
</Parent>
<Parent id="2">
<Child>Tim</Child>
<Child>Jamie</Child>
</Parent>
</Root>
Dim parents = xml.Elements
Run Code Online (Sandbox Code Playgroud)
在这种情况下,children包括所有Parent元素和所有Child元素.抓住直接后代的最佳方法是<Root>什么?
我应该编写一个LINQ查询来选择parent = <Root>?或者是否有一些我遗漏的内置方法可以为我解决这个问题?
编辑:我XElement.Elements和之间有一些混淆XElement.Descendants.正如Ruben Bartelink所指出的那样,XElement.Elements我会给我一些正在寻找的东西.
这很好用:
XDocument xdoc = new XDocument(
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test"));
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其更改为将"params数组"显式传递为数组:
object[] content = new object[] {
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test")
};
xdoc = new XDocument(content);
Run Code Online (Sandbox Code Playgroud)
它失败了:
System.ArgumentException:无法将非空白字符添加到内容中.
这两个例子不完全相同吗?这里发生了什么?
XDocument.Load(XmlReader)调用时可能抛出的异常有哪些?当文档无法提供关键信息时,很难遵循最佳实践(即避免使用通用的try catch块).
提前感谢你的帮助.
我能够在.NET中解析XML.现在我至少可以选择XmlTextReader和XDocument.这两者(或框架中包含的任何其他XML解析器)之间是否有任何比较?
也许这可以帮助我做出决定而不必深入尝试它们.
与易于使用相比,XML文件预计相当小,速度和内存使用是一个小问题.:-)
(我将从C#和/或IronPython中使用它们.)
谢谢!
如何将XDocument转换为XElement?
我通过搜索找到了以下内容,但是它用于在XDocument和XmlDocument之间进行转换,而不是XDocument和XElement.
public static XElement ToXElement(this XmlElement xmlelement)
{
return XElement.Load(xmlelement.CreateNavigator().ReadSubtree());
}
public static XmlDocument ToXmlDocument(this XDocument xdoc)
{
var xmldoc = new XmlDocument();
xmldoc.Load(xdoc.CreateReader());
return xmldoc;
}
Run Code Online (Sandbox Code Playgroud)
我找不到任何将XDocument转换为XElement的东西.任何帮助,将不胜感激.
当您创建新的XDocument使用时XDocument.Load,它是打开XML文件并保留本地副本,还是连续从硬盘驱动器读取文档?如果它连续读取,是否有更快的方法来解析XML?
XDocument x = XDocument.Load("file.xml");
Run Code Online (Sandbox Code Playgroud) 我尝试从visual studio*.csproj文件中查询元素.我创建了一个简短的例子来说明问题:
// Working
string xml1 = @"<Project ToolsVersion='4.0'>
<ItemGroup Label='Usings'>
<Reference Include='System' />
<Reference Include='System.Xml' />
</ItemGroup>
</Project>";
// Not working
string xml2 = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<ItemGroup Label='Usings'>
<Reference Include='System' />
<Reference Include='System.Xml' />
</ItemGroup>
</Project>";
XDocument doc = XDocument.Parse(xml2);
foreach (XElement element in doc.Descendants("ItemGroup"))
{
Console.WriteLine(element);
}
Run Code Online (Sandbox Code Playgroud)
字符串xml1工作正常,xml2不返回任何内容.这些字符串之间的唯一区别是文档根目录中的xmlns属性.
我如何查询包含xmlns属性的文档?当xml文档包含xmlns属性时,为什么会出现问题?
linq-to-xml ×10
xml ×9
c# ×8
.net ×5
exception ×1
performance ×1
vb.net ×1
xelement ×1
xnamespace ×1