繁体   English   中英

xml中的c#SelectSingleNode返回null。 此站点上没有类似的解决方案

[英]c# SelectSingleNode in xml returns null. There is no similar resolutions this issue at this site

我在读取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值。 有解决此问题的想法吗? 因为我尝试了很多方法,但没有结果。 谢谢

有两个问题:

1)您要一个不带名称空间SplitterName元素而XML文件中的元素隐式使用名称空间URI "http://tempuri.org/SplitterLayoutDataSet.xsd"

2)你问直接在文档元素下一个节点,你SplitterName元素不是直接孩子SplitterLayoutDataSet

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

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

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

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

“ SplitterName” xpath将在根元素下检查该元素。

采用

“ // SplitterName”或“ // SplitterLayout / SplitterName”或“ \\ SplitterLayoutDataSet / SplitterLayout / SplitterName”

AND删除xmlns =“ zzz”或将NamespaceManager添加到具有添加名称空间和前缀XPath元素的查询中,例如“ // ns:SplitterName”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM