将 Xml 节点子元素作为字典检索

发布于 2024-10-10 18:59:37 字数 2274 浏览 1 评论 0原文

我想检索以下 xml 的子元素(属性)作为字典 使用 LINQ 到 XML?

因此,当调用时,

Dictionary<String,String> dict = ReadFromXml("TC_001","L3")  

我应该能够检索 ControlList id="TC_001" 的 Control uid="L3" 作为 字典中的名称、值对为

["id","googlelink"]
[“姓名”,空]
[“类”,空]
.
.

<?xml version="1.0"?>
<ControlList id="TC_001">
    <Control uid="L1">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
        <Property name="Href"> <![CDATA["http://www.google.com/"]]></Property>  
    </Control>
    <Control uid="L2">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Class"> null </Property> 
        <Property name="ControlDefinition"> <![CDATA["id=googlelink href=\"http://www.google.co"]]> </Property>
        <Property name="TagInstance"> 1 </Property> 
    </Control>
    <Control uid="L3">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Target"> null </Property>
        <Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>        
    </Control>
</ControlList>

编辑 8/1 :同样的问题不同的 xml 结构。 (节点现在没有名称“Property”)

    <?xml version="1.0"?>
    <ControlList id="TC_001">
        <Control uid="L1">
            <Id> <![CDATA[googlelink]]><Id>
            <Name> null </Name>
            <Target> null </Target>
                   <Innertext> <![CDATA["Try searching me www.google.com"]]></Innertext>
        </Control>
             <!-- more multiple controls similar to above-->
 </ControlList>

I want to retrieve the child element (attributes) of the following xml as a Dictionary
using LINQ-to-XML??

So when called

Dictionary<String,String> dict = ReadFromXml("TC_001","L3")  

i should be able to retrieve Control uid="L3" of ControlList id="TC_001" as
name,value pair in dictionary as

["id","googlelink"]
["name",null]
["class",null]
.
.
.

<?xml version="1.0"?>
<ControlList id="TC_001">
    <Control uid="L1">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
        <Property name="Href"> <![CDATA["http://www.google.com/"]]></Property>  
    </Control>
    <Control uid="L2">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Class"> null </Property> 
        <Property name="ControlDefinition"> <![CDATA["id=googlelink href=\"http://www.google.co"]]> </Property>
        <Property name="TagInstance"> 1 </Property> 
    </Control>
    <Control uid="L3">
        <Property name="Id"> <![CDATA[googlelink]]></Property>
        <Property name="Name"> null </Property>
        <Property name="Target"> null </Property>
        <Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>        
    </Control>
</ControlList>

Edit 8/1 : Same question different xml structure.
(nodes dont have name "Property" now)

    <?xml version="1.0"?>
    <ControlList id="TC_001">
        <Control uid="L1">
            <Id> <![CDATA[googlelink]]><Id>
            <Name> null </Name>
            <Target> null </Target>
                   <Innertext> <![CDATA["Try searching me www.google.com"]]></Innertext>
        </Control>
             <!-- more multiple controls similar to above-->
 </ControlList>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-10-17 18:59:38

下面是一些 C# 示例:

static void Main()
{
    XDocument controls = XDocument.Load(@"..\..\XMLFile1.xml");
    string id1 = "TC_001", id2 = "L3";

    Dictionary<string, string> props =
        ReadFromXml(controls, id1, id2);
    foreach (string key in props.Keys)
    {
        Console.WriteLine("{0}: {1}", key, props[key]);
    }
}
static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements("Property")
        .ToDictionary(p => (string)p.Attribute("name"),
        p => { string value = p.Value.Trim(); return value == "null" ? null : value; });
}

假设传入的 id 始终存在于传入的 XML 中,否则 First() 调用将引发异常。

[编辑]对于更改后的 XML 结构,您可以使用该方法的以下改编:

static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements()
        .ToDictionary(el => el.Name.LocalName,
        el => { string value = el.Value.Trim(); return value == "null" ? null : value; });
}

Here is some C# sample:

static void Main()
{
    XDocument controls = XDocument.Load(@"..\..\XMLFile1.xml");
    string id1 = "TC_001", id2 = "L3";

    Dictionary<string, string> props =
        ReadFromXml(controls, id1, id2);
    foreach (string key in props.Keys)
    {
        Console.WriteLine("{0}: {1}", key, props[key]);
    }
}
static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements("Property")
        .ToDictionary(p => (string)p.Attribute("name"),
        p => { string value = p.Value.Trim(); return value == "null" ? null : value; });
}

That assumes the ids passed in are always present in the XML passed in, otherwise the First() calls will throw an exception.

[edit]For the changed XML structure you could use the following adaption of the method:

static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements()
        .ToDictionary(el => el.Name.LocalName,
        el => { string value = el.Value.Trim(); return value == "null" ? null : value; });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文