Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中从XML获取元素值#_C#_Xml_Microsoft Metro_Windows Store Apps - Fatal编程技术网

C# 在C中从XML获取元素值#

C# 在C中从XML获取元素值#,c#,xml,microsoft-metro,windows-store-apps,C#,Xml,Microsoft Metro,Windows Store Apps,这是文件中的XML数据 <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" /> <item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" /> <item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xm

这是文件中的XML数据

<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
<item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />
<item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
<item id="WTOC" href="TOC.html" media-type="application/xhtml+xml" />


我想使用C#店内应用程序获取元素值。 我得到了这些值,但这不是正确的方法&我无法进入下一步

    string fileContents3 = await FileIO.ReadTextAsync(file);
    xmlDoc1.LoadXml(fileContents3);
    XmlNodeList item = xmlDoc1.GetElementsByTagName("item");
    for (uint k = 0; k < item.Length; k++)
    {
        XmlElement ele1 = (XmlElement)item.Item(k);
        var attri1 = ele1.Attributes;
        var attrilist1 = attri1.ToArray();
        for (int l = 0; l < attrilist1.Length; l++)
        {
            if (attrilist1[l].NodeName == "id")
            {

                    ids2 = attrilist1[0].NodeValue.ToString();
                    ids3 = attrilist1[1].NodeValue.ToString();
            }
        } 
   }
string fileContents3=await FileIO.ReadTextAsync(文件);
xmlDoc1.LoadXml(fileContents3);
XmlNodeList item=xmlDoc1.GetElementsByTagName(“item”);
对于(uint k=0;k


与此不同,我想知道获取属性“id”的元素值的任何方法。

如果要获取
XmlNode
对象上的属性值,可以使用此函数:

    public static string GetAttributeValue(XmlNode node, string attributeName)
    {
        XmlAttribute attr = node.Attributes[attributeName];
        return (attr == null) ? null : attr.Value;
    }

如果需要使用XPath,可以按如下方式使用

      XmlDocument document = new XmlDocument();
        document.Load(@"c:\users\saravanan\documents\visual studio 2010\Projects\test\test\XMLFile1.xml");

        XmlNodeList itemNodes = document.SelectNodes("//item");

        foreach (XmlElement node in itemNodes)
        {
            if (node.Attributes.Count > 0)
            {
                if (node.HasAttribute("id"))
                {
                    Console.Write(node.Attributes["id"]);
                }

            }
        }

        var itemNodeswithAttribute = document.SelectNodes("//item[href=toc.ncx]");

        itemNodeswithAttribute = document.SelectNodes("//item[@href='toc.ncx']");
string fileContents3=await FileIO.ReadTextAsync(文件);
xmlDoc1.LoadXml(fileContents3);
XmlNodeList item=xmlDoc1.GetElementsByTagName(“item”);
对于(uint k=0;k
我简化了你的代码。希望这对你有帮助。
我假设id属性始终位于第一个位置(索引0)。

您必须将文件内容直接加载到具有path参数的XMLDocument中。不确定您想要做什么,但如果您想查找特定id的项节点,可以使用XPath。否则,您的代码非常好,我使用foreach,然后使用XMLNode,而不是For(inti=…);我只是在需要的地方使用node.Attributes[“id”]而已。我遇到了这样的错误:找不到类型或命名空间名称“XmlNode”(您是否缺少using指令或程序集引用?),可能您缺少cs文件开头的
using System.Xml
。谢谢。这对我还不起作用。我希望它用于Windows应用商店应用程序。这些属性不同。谢谢。但我希望它用于Windows应用商店应用程序。这些属性是不同的,它适合我。但在此之后,我想将这些值与同一文件中的另一个标记值进行比较
如何比较
item
元素中的
id
值和
itemref
标记中的
id
string fileContents3 = await FileIO.ReadTextAsync(file);
xmlDoc1.LoadXml(fileContents3);
XmlNodeList item = xmlDoc1.GetElementsByTagName("item");
        for (uint k = 0; k < item.Length; k++)
        {
            XmlElement ele1 = (XmlElement)item.Item(k);
            string foo = ele1.Attributes[0].NodeValue.ToString();
        }