1

我在读取 XML 字符串时遇到了问题。在这里,我有以下一个:

<SplitterLayoutDataSet xmlns="http://tempuri.org/SplitterLayoutDataSet.xsd"> 
    <SplitterLayout> 
        <SplitterName>mainSplitContainerControl</SplitterName> 
            <SplitterPosition>0.2213375796178344</SplitterPosition> 
     </SplitterLayout> 
</SplitterLayoutDataSet>

所以,我想用<SplitterName>以下代码读取标签:

XmlElement rootElement = doc.DocumentElement;
rootElement.RemoveAllAttributes();
if (rootElement != null)
    {
        foreach (KeyValuePair<Control, object> key in SaveLayoutControls)
        {
            Control c = key.Key;
            XmlElement el = rootElement.SelectSingleNode("SplitterName") as XmlElement;
            if (el != null)
            {
                if (c is GridControl)
                    SetGridLayout(el, c as GridControl);
                else if (c is SplitContainerControl)
                    SetSplitContainerLayout(el, c as SplitContainerControl);
                else if (c is TreeList)
                    SetTreeListLayout(el, c as TreeList);
                else if (c is CollapsibleSplitter)
                    SetCollapsibleSplitterLayout(el, c as CollapsibleSplitter);
                else if (c is Splitter)
                    SetSplitterLayout(el, c as Splitter);
                }
            }
       }

我想读取“el”字段,但它返回 NULL 值。有什么想法可以解决这个问题吗?因为我尝试了很多方法都没有结果。谢谢

4

3 回答 3

2

有两个问题:

1)您要求一个没有命名空间SplitterName的元素,而 XML 文件中的元素隐式使用命名空间 URI 。"http://tempuri.org/SplitterLayoutDataSet.xsd"

2)您要求直接在文档元素下的节点,并且您的SplitterName元素不是SplitterLayoutDataSet.

您绝对可以为此使用 XPath XmlNamespaceManager,但我个人会尝试使用 LINQ to XML:

XNamespace ns = "http://tempuri.org/SplitterLayoutDataSet.xsd";
XElement root = ...;
XElement names = root.Descendants(ns + "SplitterName").First();

(此外,当您没有key在搜索中的任何位置使用时,不清楚为什么您在每次迭代中都搜索该元素......)

于 2013-04-02T18:15:41.660 回答
0

您的 XPath 没有指向正确的位置。

"SplitterName" xpath 将检查根元素下的该元素。

采用

"//SplitterName" 或 "//SplitterLayout/SplitterName" 或 "\SplitterLayoutDataSet/SplitterLayout/SplitterName"

并删除 xmlns="zzz" 或将 NamespaceManager 添加到添加了命名空间并以 XPath 元素为前缀的查询中,例如“//ns:SplitterName”

于 2013-04-02T18:15:22.063 回答
0

好的,我找到了解决方案。我尝试使用命名空间,现在这部分代码可以正常工作。这是以下代码示例;

    XmlDocument doc = new XmlDocument();
    doc.InnerXml = xml;
    XmlElement rootElement = doc.DocumentElement as XmlElement;
    XmlNamespaceManager xns = new XmlNamespaceManager(doc.NameTable);
    xns.AddNamespace("ns", "http://tempuri.org/SplitterLayoutDataSet.xsd");
    if (rootElement != null)
    {
        foreach (KeyValuePair<Control, object> key in SaveLayoutControls)
        {
            Control c = key.Key;
            XmlElement el = rootElement.SelectSingleNode("ns:SplitterLayout", xns) as XmlElement;
            if (el != null)
            {
                if (c is GridControl)
                    SetGridLayout(el, c as GridControl);
                else if (c is SplitContainerControl)
                    SetSplitContainerLayout(el, c as SplitContainerControl);
                else if (c is TreeList)
                    SetTreeListLayout(el, c as TreeList);
                else if (c is CollapsibleSplitter)
                    SetCollapsibleSplitterLayout(el, c as CollapsibleSplitter);
                else if (c is Splitter)
                    SetSplitterLayout(el, c as Splitter);
            }
        }
    }
于 2013-04-03T13:05:12.667 回答