1

对某人来说似乎很愚蠢。我最近几天一直在努力,但我还没有成功。我使用 c# 为 Windows Store 应用程序工作。我正在尝试从本地解析 XMl 文件。

    <?xml version="1.0" ?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
   <dc:title>Cinderella; Or The Little Glass Slipper</dc:title>
   <dc:creator>Anonymous</dc:creator>
   <dc:date>2009-10-14</dc:date>
   <dc:subject>Youth</dc:subject>
   <dc:language>en</dc:language>
   <dc:publisher>Web Books Publishing</dc:publisher>
   <dc:identifier id="BookId">web-books-154</dc:identifier>
 </metadata>
 <manifest>
     <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" />
    <item id="style" href="style.css" media-type="text/css" />
    <item id="cover" href="cover.jpg" media-type="image/jpeg" />
      <item id="Wimg11" href="images/img11.jpg" media-type="image/jpeg" />
      <item id="Wimg12" href="images/img12.jpg" media-type="image/jpeg" />
      <item id="Wimg2" href="images/img2.jpg" media-type="image/jpeg" />
      <item id="Wimg4" href="images/img4.jpg" media-type="image/jpeg" />
      <item id="Wimg6" href="images/img6.jpg" media-type="image/jpeg" />
      <item id="Wimg7" href="images/img7.jpg" media-type="image/jpeg" />
      <item id="Wimg9" href="images/img9.jpg" media-type="image/jpeg" />
 </manifest>
  <spine toc="ncx">
    <itemref idref="W000Title" />
    <itemref idref="W01MB154" />
 </spine>
</package>

从上面的 Xml 文件中,我想要在 For this I am 中的 href 值XmlDocument()。这是我的 C# 代码。

XmlNodeList itemref = xmlDoc1.GetElementsByTagName("itemref");
            foreach (XmlElement idref in itemref)
            {
                if (idref.Attributes.Count > 0)
                {
                    XmlAttribute Str1 = idref.GetAttributeNode("idref");
                    ids1list.Add(Str1.Value); //List
          }
            }
XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("item");
           foreach (XmlElement item in itemNodes)
            {
                    XmlAttribute str2 = item.GetAttributeNode("id");
                    ids2list.Add(str2.value);
                    XmlAttribute str3 = item.GetAttributeNode("href");
                    ids3list.Add(str3.value);
             }
            var resut = ids1list.Intersect(ids2list).ToList();

结果列表中,我有id元素的值。我想要下一个值,即使用id 值的href,有没有办法获得下一个元素值,。谢谢。

4

2 回答 2

3

您可以通过以下代码执行此操作,

            XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("item");
            if (itemNodes.Count > 0)
            {
               foreach (XmlElement node in itemNodes)
               {
                 ids3list.Add(node.Attributes["href"].Value);
               }
            }
于 2013-07-01T10:41:55.077 回答
0

对于 Windows Store XmlDocument 读取/写入属性值:

XmlAttribute attr = (XmlAttribute)attrColl.GetNamedItem("href");
attr.Value = "images/img11.jpg";
or
string val = attr.Value;

MSDN:XmlDocument.Attributes

于 2014-07-18T17:22:14.930 回答