English 中文(简体)
从属性价值中获取国际投资倡议的最简单途径?
原标题:Simplest way to get a URI from an attribute value?

我在我的XML中有一个类似内容。

<myElement myAttribute="myNamespacePrefix:foobar"/>

该档案中有一只Xmlns声明:

<xmlns:myNamespacePrefix="http://www.mydomain.com/blah#"/>

I want to make a URI from the value of myAttribute.

我想:

http://www.mydomain.com/blah#foobar

Sounds simple, doesn t it?

因此,简单的解决办法是什么?

我在《框架》或“MSDN”中找不到任何东西,因此,我只得写这一无一不在的泡影:

public static Uri GetUri(string value, XElement containingElement)
        {
            if (value.Contains(":"))
            {

                var prefix = String.Concat(value.TakeWhile(c => c !=  : ));
                XNamespace ns;
                if (containingElement.GetNamespaceOfPrefix(prefix) != null)
                    ns = containingElement.GetNamespaceOfPrefix(prefix);
                else ns = containingElement.GetDefaultNamespace();
                var localName = String.Concat(value.SkipWhile(c => c !=  : ).Skip(1));
                Contract.Assert(ns != null);
                if (String.IsNullOrWhiteSpace(ns.NamespaceName))
                    return new Uri(value);  
                else
                {
                    StringBuilder sb = new StringBuilder(ns.NamespaceName);
                    if (!ns.NamespaceName.EndsWith("#"))
                    {
                        sb.Append( # );
                    }

                    return new Uri(String.Format(CultureInfo.CurrentCulture, "{0}{1}", sb.ToString(), localName));
                } 

            }
            else // no namespace prefix
            {
                String fragment;
                if (!value.StartsWith("#"))
                    fragment = value.Insert(0, "#");
                else
                    fragment = value;
                var uriString = String.Format(CultureInfo.CurrentCulture, "{0}{1}", containingElement.BaseUri, fragment);
                return new Uri(uriString);
            }
        }
问题回答

我最近做了一些与XML有关的 st。 这是我从C#中XML获取信息的一个实例:

XmlDocument XML = new XmlDocument();
XML.LoadXml(/* XML data in string format goes here */);

然后,你可以做如下事情:

if (XML["myElement"] != null)
{
    string myAttributeStr = XML["myElement"].Attributes["myAttribute"].Value.ToString().Trim();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签