在 Linq (C#) 中处理 xml:计算后代/元素并检查计数 = X
给定 XML 文档:
<Root xmlns="http://foo" xmlns:bar="http://bar">
<Child Name="one" bar:Type="type1" />
<Child Name="two" bar:Caption="captionvalue" />
</Root>
使用 XElement 根(具有元素 Root)执行以下操作的最有效方法是什么:
- 确保只有两个子元素作为后代
- 对于每个子元素中的每个属性,如果命名空间为 http://bar 然后调用 BarFunction(attribute.value) 否则调用 NoBarFunction(attribute.value)
- 如果我需要以不同的方式处理 Child 的每个属性基于他们的名字(例如:用值填充一组变量)是 foreach (Attribute) 块内 Attribute.Name 上的 if-else 的唯一选项吗?
我是否正确地假设没有“http://bar”命名空间的属性实际上位于某个默认命名空间中(而不是位于 http:// /foo) 与如果不合格则继承父级命名空间的元素不同?
我有:
IEnumerable<XElement> children = root.Elements("Child");
// How do I test the count? And get each Child
foreach (XAttribute attribute in child.Attributes())
{
if (attribute.Name.Namespace.ToString() == "http://bar")
{
BarFunction(attribute.Value);
}
else //Default Namespace
{
NoBarFunction(attribute.Value);
}
}
Given the XML document:
<Root xmlns="http://foo" xmlns:bar="http://bar">
<Child Name="one" bar:Type="type1" />
<Child Name="two" bar:Caption="captionvalue" />
</Root>
What is the most efficient way to do the following using the XElement root (that has the element Root):
- Make sure there are only two Child elements as descendents
- For each attribute in each Child element, if namespace is http://bar then call BarFunction(attribute.value) else call NoBarFunction(attribute.value)
- If I need to process each attribute of Child differently based on their Name (eg: populate a set of variables with the values) is the only option an if-else on Attribute.Name inside the foreach (Attribute) block ?
Am I correct in assuming that attribute that do not have the "http://bar" namespace are actually in some default namespace (and not in http://foo) unlike the elements who inherit the namespace of the parent if not qualified?
I have:
IEnumerable<XElement> children = root.Elements("Child");
// How do I test the count? And get each Child
foreach (XAttribute attribute in child.Attributes())
{
if (attribute.Name.Namespace.ToString() == "http://bar")
{
BarFunction(attribute.Value);
}
else //Default Namespace
{
NoBarFunction(attribute.Value);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 LINQ 将是最有效的方法。类似于(凭记忆):
然后对
结果
执行检查以查看命名空间是什么,然后对该/那些 XElement 执行该方法。这也可以在 LINQ 中使用与上面类似的语法来完成。对于第 3 点,您可以使用
switch
块,这可能更合适,尽管架构由您决定。例如,如果元素是一组预定义值,那么您还可以使用 LINQ 来填充变量。Using LINQ would be the most efficient way. Something like (from memory):
And then performing your check on your
results
to see what the namespace is and then performing the method on that/those XElement(s). This could also be done in LINQ using similar syntax to above.For point 3, you could use a
switch
block which would probably be more appropriate, though it is up to you to decide on the architecture. E.g. if the elements are a set of predefined values, then you could also use LINQ to populate your variables.API:Extensions.GetSchemaInfo
思考在框中,您想知道在哪个模式中声明了属性(或元素或...)。
在“XML 读取”角色中,这将是哪个模式验证该节点。
用验证解析器术语来说,此信息是 PVSI(验证后架构信息集)的一部分。您可以通过调用来获取该信息,例如
使用
addSchemaInfo< /code> 设置为 true
。
如果 addSchemaInfo 为 true,则此方法将使用架构验证后信息集 (PSVI) 填充 XML 树。
使用 PSVI 填充 XML 树需要两个步骤。
首先,将注释添加到树中的所有节点,以便您可以对树中的元素或属性调用 Extensions.GetSchemaInfo 或 Extensions.GetSchemaInfo。
其次,XSD 中定义的默认元素和属性将添加到 XML 树中。通过调用 GetSchemaInfo 方法之一,您可以确定是否从 XSD 添加了特定元素或属性作为默认元素或属性。
API: Extensions.GetSchemaInfo
Thinking out of the box, you want to know which schema an attribute (or element or...) was declared in.
In the 'XML reading' role, that would be which schema validated that node.
In validating parser jargon this information is part of the PVSI (Post Validation Schema Infoset). You can get to that information by calling, e.g.
With
addSchemaInfo
set to true.If addSchemaInfo is true, this method populates the XML tree with the post-schema-validation infoset (PSVI).
There are two steps to populating the XML tree with the PSVI.
First, an annotation is added to all nodes in the tree to enable you to call Extensions.GetSchemaInfo or Extensions.GetSchemaInfo on an element or attribute in the tree.
Second, default elements and attributes defined in the XSD are added to the XML tree. By calling one of the GetSchemaInfo methods, you can determine if a specific element or attribute was added from the XSD as a default element or attribute.