LINQ to XML iqnoreすべての名前空間を使用するにはどうすればよいですか?または、代わりに、名前空間を削除するにはどうすればよいですか?
名前空間が半ランダムに設定されており、名前空間がある場合とない場合の両方でノードを検索する必要があるので、私は尋ねています。
回答:
書く代わりに:
nodes.Elements("Foo")
書く:
nodes.Elements().Where(e => e.Name.LocalName == "Foo")
それに飽きたら、独自の拡張メソッドを作成します。
public static IEnumerable<XElement> ElementsAnyNS<T>(this IEnumerable<T> source, string localName)
where T : XContainer
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}
名前空間付き属性を頻繁に処理する必要がある場合は、属性についても同様です(これは比較的まれです)。
XPathの場合、次のように記述します。
/foo/bar | /foo/ns:bar | /ns:foo/bar | /ns:foo/ns:bar
あなたはlocal-name()
機能を使うことができます:
/*[local-name() = 'foo']/*[local-name() = 'bar']
xDoc.Root.Descendants().Where(e => e.Name.LocalName == "SomeName");
名前空間を削除する方法は次のとおりです。
private static XElement StripNamespaces(XElement rootElement)
{
foreach (var element in rootElement.DescendantsAndSelf())
{
// update element name if a namespace is available
if (element.Name.Namespace != XNamespace.None)
{
element.Name = XNamespace.None.GetName(element.Name.LocalName);
}
// check if the element contains attributes with defined namespaces (ignore xml and empty namespaces)
bool hasDefinedNamespaces = element.Attributes().Any(attribute => attribute.IsNamespaceDeclaration ||
(attribute.Name.Namespace != XNamespace.None && attribute.Name.Namespace != XNamespace.Xml));
if (hasDefinedNamespaces)
{
// ignore attributes with a namespace declaration
// strip namespace from attributes with defined namespaces, ignore xml / empty namespaces
// xml namespace is ignored to retain the space preserve attribute
var attributes = element.Attributes()
.Where(attribute => !attribute.IsNamespaceDeclaration)
.Select(attribute =>
(attribute.Name.Namespace != XNamespace.None && attribute.Name.Namespace != XNamespace.Xml) ?
new XAttribute(XNamespace.None.GetName(attribute.Name.LocalName), attribute.Value) :
attribute
);
// replace with attributes result
element.ReplaceAttributes(attributes);
}
}
return rootElement;
}
使用例:
XNamespace ns = "http://schemas.domain.com/orders";
XElement xml =
new XElement(ns + "order",
new XElement(ns + "customer", "Foo", new XAttribute("hello", "world")),
new XElement("purchases",
new XElement(ns + "purchase", "Unicycle", new XAttribute("price", "100.00")),
new XElement("purchase", "Bicycle"),
new XElement(ns + "purchase", "Tricycle",
new XAttribute("price", "300.00"),
new XAttribute(XNamespace.Xml.GetName("space"), "preserve")
)
)
);
Console.WriteLine(xml.Element("customer") == null);
Console.WriteLine(xml);
StripNamespaces(xml);
Console.WriteLine(xml);
Console.WriteLine(xml.Element("customer").Attribute("hello").Value);
属性の名前空間を無視する簡単な方法を探してこの質問を見つけたので、Pavelの回答に基づいて、属性にアクセスするときに名前空間を無視するための拡張機能を次に示します(コピーを簡単にするために、彼の拡張機能を含めました)。
public static XAttribute AttributeAnyNS<T>(this T source, string localName)
where T : XElement
{
return source.Attributes().SingleOrDefault(e => e.Name.LocalName == localName);
}
public static IEnumerable<XElement> ElementsAnyNS<T>(this IEnumerable<T> source, string localName)
where T : XContainer
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}