লিনকু থেকে এক্সএমএলে নেমস্পেসগুলি উপেক্ষা করুন


87

আমি কীভাবে সমস্ত নেমস্পেসগুলিতে লিনিকিউ থেকে এক্সএমএল থাকতে পারি? অথবা পর্যায়ক্রমে, আমি কীভাবে নামের স্থানগুলি ছড়িয়ে দেব?

আমি জিজ্ঞাসা করছি কারণ নেমস্পেসগুলি একটি আধা-এলোমেলো ফ্যাশনে সেট করা হচ্ছে এবং আমি নেমস্পেসের সাথে এবং ছাড়াও নোডগুলি অনুসন্ধান করতে করতে ক্লান্ত হয়েছি।


উত্তর:


137

লেখার পরিবর্তে:

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);
}

বৈশিষ্ট্যগুলির জন্য ডিট্টো, যদি আপনাকে প্রায়শই নেমস্পিডের বৈশিষ্ট্যগুলি মোকাবেলা করতে হয় (যা তুলনামূলকভাবে বিরল)।

[সম্পাদনা] এক্সপাথের জন্য সমাধান যুক্ত করা হচ্ছে

এক্সপাথের জন্য, লেখার পরিবর্তে:

/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");
অনন্যরূপে

17

নেমস্পেসগুলি ফেলা করার জন্য এখানে একটি পদ্ধতি রয়েছে:

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);

4

যেহেতু আমি এই প্রশ্নটি অনুসন্ধানের বৈশিষ্ট্যের সাথে নেমস্পেসগুলি উপেক্ষা করার সহজ উপায়ের সন্ধানে পেয়েছি, পাভেলের উত্তরের উপর ভিত্তি করে কোনও বৈশিষ্ট্য অ্যাক্সেস করার সময় নেমস্পেসগুলি উপেক্ষা করার জন্য এখানে একটি এক্সটেনশন দেওয়া হয়েছে (সহজ অনুলিপি করার জন্য, আমি তার প্রসারকে অন্তর্ভুক্ত করেছি):

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);
}
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.