gpt4 book ai didi

c# - 使用 XMLTextReader,我如何知道我在哪个元素上?

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:21 25 4
gpt4 key购买 nike

我的代码是这样的:

case "Creator":
br.Read();
br.MoveToContent(); // gives the content of the role
tbComposer.Text = br.Value;
br.Read();
br.MoveToContent(); // gives the content of the role
tbConductor.Text = br.Value;
br.Read();
br.MoveToContent(); // gives the content of the role
tbOrchestra.Text = br.Value;
break;

这是工作代码:(感谢大家的输入……没有你我做不到!)Spokane-Dude

                case "Creator":
br.MoveToFirstAttribute();
if (br.Value == "Composer") {
br.Read();
tbComposer.Text = br.Value;
}
if (br.Value == "Conductor") {
br.Read();
tbConductor.Text = br.Value;
}
if (br.Value == "Orchestra") {
br.Read();
tbOrchestra.Text = br.Value;
}
break;

这是我的 XML 的样子:

<ItemLookupResponse>
<OperationRequest/>
<Items>
<Request/>
<Item>
<ItemAttributes>
<Binding>Audio CD</Binding>
<CatalogNumberList>
<CatalogNumberListElement>43850</CatalogNumberListElement>
</CatalogNumberList>
<Creator Role="Composer">Gioachino Rossini</Creator>
<Creator Role="Conductor">Riccardo Chailly</Creator>
<Creator Role="Orchestra">National Philharmonic Orchestra</Creator>
</ItemAttributes>
</Item>
</Items>
</ItemLookupResponse>

我需要知道我正在阅读元素 Creator Role="Composer" 还是 Creator Role="Conductor"

那么,使用 XMLTextReader,我如何确定元素文本是什么?

最佳答案

这个样本怎么样?希望对你有用

   static void Main(string[] args)
{

string xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Creators><Creator Role=\"Composer\">Gioachino Rossini</Creator><Creator Role=\"Conductor\">Riccardo Chailly</Creator><Creator Role=\"Orchestra\">National Philharmonic Orchestra</Creator></Creators>";
using (XmlReader xmlReader = XmlTextReader.Create(new StringReader(xmlStr)))
{
xmlReader.MoveToContent();
xmlReader.ReadStartElement("Creators" , "");
SomeMethod("Composer", xmlReader);
SomeMethod("Conductor", xmlReader);
SomeMethod("Orchestra", xmlReader);
}

Console.WriteLine("........");
Console.Read();
}

static void SomeMethod(string role, XmlReader xmlReader)
{
xmlReader.MoveToAttribute("Role");

switch (role)
{
case "Composer":
{
xmlReader.MoveToContent();
Console.WriteLine(string.Format("Composer:{0}", xmlReader.ReadElementContentAsString()));

} break;
case "Conductor":
{
xmlReader.MoveToContent();
Console.WriteLine(string.Format("Conductor:{0}", xmlReader.ReadElementContentAsString()));

} break;
case "Orchestra":
{
xmlReader.MoveToContent();
Console.WriteLine(string.Format("Orchestra:{0}", xmlReader.ReadElementContentAsString()));

} break;

default: break;
}
}

关于c# - 使用 XMLTextReader,我如何知道我在哪个元素上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8205706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com