XMLReader 读取到未知类型的下一个兄弟
XMLReader read to next sibling of unknown type
我了解我们如何使用如下代码示例..
public class Sample
{
public static void Main()
{
using (XmlReader reader = XmlReader.Create("books.xml"))
{
reader.ReadToFollowing("book");
do
{
Console.WriteLine("Name ", reader.GetAttribute("Name"));
} while (reader.ReadToNextSibling("book"));
}
}
}
这将读取每个 "book" 类型的兄弟。所以在像下面这样的 xml 结构中它会工作得很好..
<Section>
<book Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<book Name="Adventure Willy"/>
<book Name="Mr. G and the Sandman"/>
<book Name="Terry and Me"/>
</Section>
但是假设您的兄弟姐妹并不总是书籍类型。.在内部部分,我们可以有书籍、cd、dvd 或 vhs,所以 xml 可能看起来像这样
<Section>
<cd Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<vhs Name="Adventure Willy"/>
<cd Name="Mr. G and the Sandman"/>
<dvd Name="Terry and Me"/>
</Section>
我想写一个方法,无论它是什么兄弟类型,它都会给我 Name 属性。使用上面的代码我只会得到 [Titan Quest 2]。这可以做到吗?谢谢!
试试下面的代码。适用于两种类型的书籍。我结合使用了 xmlreader 和 XElement :
这是我的xml
<test>
<Section>
<book Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<book Name="Adventure Willy"/>
<book Name="Mr. G and the Sandman"/>
<book Name="Terry and Me"/>
</Section>
<Section>
<cd Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<vhs Name="Adventure Willy"/>
<cd Name="Mr. G and the Sandman"/>
<dvd Name="Terry and Me"/>
</Section>
</test>
这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List<List<KeyValuePair<string, string>>> books = new List<List<KeyValuePair<string, string>>>();
using (XmlReader reader = XmlReader.Create(FILENAME))
{
while (!reader.EOF)
{
if (reader.Name != "Section")
{
reader.ReadToFollowing("Section");
}
if (!reader.EOF)
{
XElement section = (XElement)XElement.ReadFrom(reader);
List<KeyValuePair<string, string>> book = new List<KeyValuePair<string, string>>();
books.Add(book);
foreach (XElement b in section.Elements())
{
book.Add(new KeyValuePair<string, string>(b.Name.LocalName, (string)b.Attribute("Name")));
}
}
}
}
}
}
}
这是结果
将此代码与 XDocument 一起使用将从属性中获取所有值:
var document = XDocument.Load("test.xml");
var bookValues = document.XPathSelectElement("Section")
.Descendants()
.Select(x => x.Attribute("Name").Value);
或使用 XmlReader 获取所有属性值:
List<String> values = new List<string>();
using (var xmlreader = XmlReader.Create("test.xml"))
{
xmlreader.ReadToDescendant("Section");
while (xmlreader.Read())
{
if (xmlreader.IsStartElement())
{
values.Add(xmlreader.GetAttribute("Name"));
}
}
}
我了解我们如何使用如下代码示例..
public class Sample
{
public static void Main()
{
using (XmlReader reader = XmlReader.Create("books.xml"))
{
reader.ReadToFollowing("book");
do
{
Console.WriteLine("Name ", reader.GetAttribute("Name"));
} while (reader.ReadToNextSibling("book"));
}
}
}
这将读取每个 "book" 类型的兄弟。所以在像下面这样的 xml 结构中它会工作得很好..
<Section>
<book Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<book Name="Adventure Willy"/>
<book Name="Mr. G and the Sandman"/>
<book Name="Terry and Me"/>
</Section>
但是假设您的兄弟姐妹并不总是书籍类型。.在内部部分,我们可以有书籍、cd、dvd 或 vhs,所以 xml 可能看起来像这样
<Section>
<cd Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<vhs Name="Adventure Willy"/>
<cd Name="Mr. G and the Sandman"/>
<dvd Name="Terry and Me"/>
</Section>
我想写一个方法,无论它是什么兄弟类型,它都会给我 Name 属性。使用上面的代码我只会得到 [Titan Quest 2]。这可以做到吗?谢谢!
试试下面的代码。适用于两种类型的书籍。我结合使用了 xmlreader 和 XElement :
这是我的xml
<test>
<Section>
<book Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<book Name="Adventure Willy"/>
<book Name="Mr. G and the Sandman"/>
<book Name="Terry and Me"/>
</Section>
<Section>
<cd Name="Titan Quest 1"/>
<book Name="Titan Quest 2"/>
<vhs Name="Adventure Willy"/>
<cd Name="Mr. G and the Sandman"/>
<dvd Name="Terry and Me"/>
</Section>
</test>
这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List<List<KeyValuePair<string, string>>> books = new List<List<KeyValuePair<string, string>>>();
using (XmlReader reader = XmlReader.Create(FILENAME))
{
while (!reader.EOF)
{
if (reader.Name != "Section")
{
reader.ReadToFollowing("Section");
}
if (!reader.EOF)
{
XElement section = (XElement)XElement.ReadFrom(reader);
List<KeyValuePair<string, string>> book = new List<KeyValuePair<string, string>>();
books.Add(book);
foreach (XElement b in section.Elements())
{
book.Add(new KeyValuePair<string, string>(b.Name.LocalName, (string)b.Attribute("Name")));
}
}
}
}
}
}
}
这是结果
将此代码与 XDocument 一起使用将从属性中获取所有值:
var document = XDocument.Load("test.xml");
var bookValues = document.XPathSelectElement("Section")
.Descendants()
.Select(x => x.Attribute("Name").Value);
或使用 XmlReader 获取所有属性值:
List<String> values = new List<string>();
using (var xmlreader = XmlReader.Create("test.xml"))
{
xmlreader.ReadToDescendant("Section");
while (xmlreader.Read())
{
if (xmlreader.IsStartElement())
{
values.Add(xmlreader.GetAttribute("Name"));
}
}
}