我在将XML解析为数据库时遇到了问题。我的XML结构看起来像这样:
<atrists>
<artist>
<images>
<image/>
</images>
<id>
</id>
</artist>
</artists>
有很多艺术家的标签。而在艺术家中有很多标签。
我现在在做什么:
while (reader.ReadToFollowing("artist"))
{
XmlReader reader2 = reader.ReadSubtree();
while (reader2.Read())
{
if (reader2.NodeType == XmlNodeType.Element)
{
if (reader2.Name == "images")
{
while (reader2.ReadToFollowing("image"))
{
//here i am adding all images data to lists to query it later
}
}
if (reader2.Name == "id")
{
id = reader2.ReadElementContentAsInt();
}
}
//这里我想做一个SQL查询,它会添加许多图像和ID,并附加到每个图像上。
}
该ID效果不佳。它跳过50%的ID或者根本没有变化。但我可以在那里看到每一个可能的形象。所以我认为从表单节点到节点有问题。有任何想法吗?
答案 0 :(得分:1)
然后试试这个:
async Task BigFileReader(System.IO.Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
bool bIdEncountered = false;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (await reader.ReadAsync())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
bIdEncountered = reader.LocalName.Equals("id");
break;
case XmlNodeType.Text:
var value = await reader.GetValueAsync();
if(bIdEncountered) Console.WriteLine("Run my SQL for {0}", value);
break;
case XmlNodeType.EndElement:
break;
default:
break;
}
}
}
}
示例2
也许这会给你更好的例子:
async Task BigFileReader(System.IO.Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
bool bIdEncountered = false;
bool bImageEncountered = false;
List<string> images = new List<string>();
int artistId = 0;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (await reader.ReadAsync())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
bIdEncountered = reader.LocalName.Equals("id");
bImageEncountered = reader.LocalName.Equals("image");
if(reader.LocalName.Equals("images")) images.Clear();
break;
case XmlNodeType.Text:
//var value = await reader.GetValueAsync();
if (bIdEncountered) artistId = Convert.ToInt32(await reader.GetValueAsync());
if (bImageEncountered) images.Add(await reader.GetValueAsync());
break;
case XmlNodeType.EndElement:
if (reader.LocalName.Equals("artist")) Console.WriteLine("Saving artist {0} with images {1}", artistId, String.Join(",", images));
break;
default:
break;
}
}
}
}
带属性的示例
async Task BigFileReader(System.IO.Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
bool bIdEncountered = false;
List<ImageNode> images = new List<ImageNode>();
int artistId = 0;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (await reader.ReadAsync())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
bIdEncountered = reader.LocalName.Equals("id");
//bImageEncountered = reader.LocalName.Equals("image");
if(reader.LocalName.Equals("images")) images.Clear();
if (reader.LocalName.Equals("image"))
{
images.Add( new ImageNode
{
Width = Convert.ToInt32(reader.GetAttribute("width")),
Height = Convert.ToInt32(reader.GetAttribute("height")),
Url = reader.GetAttribute("uri")
});
}
break;
case XmlNodeType.Text:
if (bIdEncountered) artistId = Convert.ToInt32(await reader.GetValueAsync());
break;
case XmlNodeType.EndElement:
if (reader.LocalName.Equals("artist")) Console.WriteLine("Saving artist {0} with images {1}", artistId, String.Join(",", images));
break;
default:
break;
}
}
}
}
internal class ImageNode
{
public int Width { get; set; }
public int Height { get; set; }
public string Url { get; set; }
public override string ToString() { return String.Format("{0}x{1}:{2}", Width, Height, Url); }
}
同步版本(VS2010)
static void BigFileReader(System.IO.Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
bool bIdEncountered = false;
List<ImageNode> images = new List<ImageNode>();
int artistId = 0;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
bIdEncountered = reader.LocalName.Equals("id");
//bImageEncountered = reader.LocalName.Equals("image");
if (reader.LocalName.Equals("images")) images.Clear();
if (reader.LocalName.Equals("image"))
{
images.Add(new ImageNode
{
Width = Convert.ToInt32(reader.GetAttribute("width")),
Height = Convert.ToInt32(reader.GetAttribute("height")),
Url = reader.GetAttribute("uri")
});
}
break;
case XmlNodeType.Text:
if (bIdEncountered) artistId = Convert.ToInt32(reader.Value);
break;
case XmlNodeType.EndElement:
if (reader.LocalName.Equals("artist")) Console.WriteLine("Saving artist {0} with images {1}", artistId, String.Join(",", images));
break;
default:
break;
}
}
}
}