C# 如何从XML中获取元素并将其发送到数组?C
我有一个小样本:C# 如何从XML中获取元素并将其发送到数组?C,c#,xml,windows,C#,Xml,Windows,我有一个小样本: <response list='true'> <count>12</count> <post> <id>307</id> <from_id>123</from_id> <to_id>123</to_id> <date>123892128</date>
<response list='true'>
<count>12</count>
<post>
<id>307</id>
<from_id>123</from_id>
<to_id>123</to_id>
<date>123892128</date>
<post_type>post</post_type>
<text>Smth TExt</text>
<attachments list='true'>
<attachment>
<type>photo</type>
<photo>
<pid>123</pid>
<aid>-7</aid>
<owner_id>123</owner_id>
<src>http://url1.jpg</src>
<src_big>http://url2.jpg</src_big>
<src_small>http://url3.jpg</src_small>
<src_xbig>http://url4.jpg</src_xbig>
<src_xxbig>http://url5.jpg</src_xxbig>
<src_xxxbig>http://url6.jpg</src_xxxbig>
<width>990</width>
<height>1188</height>
<text/>
<created>135</created>
<access_key>67</access_key>
</photo>
</attachment>
<attachment>...</attachment>
<attachment>...</attachment>
</attachments>
<comments><count>0</count></comments>
<likes><count>2</count></likes>
<reposts><count>0</count></reposts>
</post>
</response>
我该怎么做?我可以从这个-12中得到计数,但我不能得到每个帖子中元素的值。这是Windows8.1应用程序。Visual Studio 2013。
我试过2个Paul Sasik:
private void name(){
XDocument parsedoc = XDocument.Parse(herelinktoxmldoc);
foreach (XElement i in parsedoc.Root.Elements())
{
if (w==0)
{
count = Convert.ToInt32(i.Value);
}
w++;
}
count += 0;
string[] post_from_id = new String[count];
string[] post_to_id = new String[count];
string[] post_date = new String[count];
string[] post_text = new String[count];
for (int i = 0; i < count; i++)
{
post_from_id[i] = ParsingXmlwoReq(wallxmlstring, "from_id");
post_to_id[i] = ParsingXmlwoReq(wallxmlstring, "to_id");
post_date[i] = ParsingXmlwoReq(wallxmlstring, "date");
post_text[i] = ParsingXmlwoReq(wallxmlstring, "text");
}
}
}
private string ParsingXmlwoReq(string request, string whatget)
{
XDocument parsedoc = XDocument.Parse(request);
string forreturn = "Null";
var get = (from uri in parsedoc.Descendants(whatget) select uri.Value);
foreach (var element in get)
{
forreturn = System.Convert.ToString(element);
}
return forreturn;
}
您正在尝试以最复杂的方式读取XML。没有必要。内置了更简单的方法。请参阅下面的代码 对于示例代码,我将XML简化并硬编码为本地字符串,并且没有错误检查!该示例是基本示例,但如果您了解XmlDocument.SelectNodes方法及其XPath表达式参数sample/response/post,那么您可以在所选文档的任何级别重用它。无论你做什么,首先要想一想:以前有人做过这种操作吗?可能有API吗?99.9%的答案是肯定和肯定 我还建议创建一个数据类来匹配XML文档,并创建一个包含实例的列表。从代码示例来看,您似乎正在尝试将数据项添加到单独的数组中。这是不必要的复杂和危险,因为将一个数组的数据链接到下一个数组的唯一数据是索引!请参见原始示例中添加在底部的示例类结构。我保留了带有属性的XML值示例,以便轻松映射到XML文档 以下是一个方便的教程,作为入门和探索访问XML文档的其他方法的基础: 示例数据类结构:
public class Post // <post>
{
int PostId { get; set; } // <id>307</id>
int FromId { get; set; } // <<from_id>123</from_id>
int ToId { get; set; } // <<to_id>123</to_id>
DateTime PostDate { get; set; } // <<date>123892128</date>
string PostType { get; set; } // <<post_type>post</post_type>
string Text { get; set; } // <<text>Smth TExt</text>
List<PostAttachment> Attachments { get; set; } // <attachments list="true">
}
public class PostAttachment // <attachment>
{
string AttachmentType { get; set; } // <type>photo</type>
List<Photo> AttachedPhotos { get; set; } // <photo>
}
public class Photo
{
int PhotoId { get; set; } // <pid>123</pid>
int AId { get; set; } // <aid>-7</aid>
int OwnerId { get; set; } // <owner_id>123</owner_id>
string Source { get; set; } // <src>http://url1.jpg</src>
string Source1Small { get; set; } // <src_small>http://url3.jpg</src_small>
string Source2Big { get; set; } // <src_big>http://url2.jpg</src_big>
string Source3XBig { get; set; } // <src_xbig>http://url4.jpg</src_xbig>
string Source4XXBig { get; set; } // <src_xxbig>http://url5.jpg</src_xxbig>
string Source5XXXBig { get; set; } // <src_xxxbig>http://url6.jpg</src_xxxbig>
int Width { get; set; } // <width>990</width>
int Height { get; set; } // <height>1188</height>
string Text { get; set; } // <text/>
DateTime CreatedDate { get; set; } // <created>135</created>
int AccessKey { get; set; } // <access_key>67</access_key>
}
到目前为止您尝试的代码在哪里?现在的问题是:请为我做我的工作。这样的问题是不受欢迎的。我认为将此作为答案发布是不好的,但请尝试HtmlAgilityPack:它是一个用于解析和遍历XML和HTML的库。我还没有做过研究来确保它能在Windows8.1应用程序中运行,但我不明白为什么它不能运行。@ZacharyKniebel:agility pack是一个不错的工具+1,但OP不需要太多来完成我认为代码试图实现的目标。NET中已经内置了一些简单的XML API,可以非常简单地与XPath表达式一起使用。“在这种情况下,我认为外部库是过分的。”PaulSasik-我完全同意。我在他向OP添加代码之前添加了这条评论。如果我早些时候看到,我就不会用这条评论了。这就是为什么我认为用帖子作为答案是不好的@扎查里克尼贝尔:哈哈,太棒了+1实际上,我要赞扬你没有发布答案:那是在我迷失在行动和我的答案和解释之前。。。但现在我觉得自己很傻。我可能刚刚写了一百行代码,为一个初露头角的吸血鬼提供了一个很好的机会,它将被复制/粘贴到生产代码中。我自己也应该这样做-
private void ReadDoc()
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString); // your test XML (hard coded below)
XmlNodeList postList = xml.SelectNodes("/response/post");
Console.WriteLine(postList.Count);
foreach (XmlNode post in postList)
{
Console.WriteLine(post["from_id"]);
Console.WriteLine(post["to_id"]);
Console.WriteLine(post["date"]);
Console.WriteLine(post["text"]);
}
}
string xmlString = @"
<response list='true'>
<count>12</count>
<post>
<id>307</id>
<from_id>123</from_id>
<to_id>123</to_id>
<date>123892128</date>
<post_type>post</post_type>
<text>Smth TExt</text>
<attachments list='true'>
<attachment>
<type>photo</type>
<photo>
<pid>123</pid>
<aid>-7</aid>
<owner_id>123</owner_id>
<src>http://url1.jpg</src>
<src_big>http://url2.jpg</src_big>
<src_small>http://url3.jpg</src_small>
<src_xbig>http://url4.jpg</src_xbig>
<src_xxbig>http://url5.jpg</src_xxbig>
<src_xxxbig>http://url6.jpg</src_xxxbig>
<width>990</width>
<height>1188</height>
<text/>
<created>135</created>
<access_key>67</access_key>
</photo>
</attachment>
<attachment>...</attachment>
<attachment>...</attachment>
</attachments>
<comments><count>0</count></comments>
<likes><count>2</count></likes>
<reposts><count>0</count></reposts>
</post>
</response>
";
public class Post // <post>
{
int PostId { get; set; } // <id>307</id>
int FromId { get; set; } // <<from_id>123</from_id>
int ToId { get; set; } // <<to_id>123</to_id>
DateTime PostDate { get; set; } // <<date>123892128</date>
string PostType { get; set; } // <<post_type>post</post_type>
string Text { get; set; } // <<text>Smth TExt</text>
List<PostAttachment> Attachments { get; set; } // <attachments list="true">
}
public class PostAttachment // <attachment>
{
string AttachmentType { get; set; } // <type>photo</type>
List<Photo> AttachedPhotos { get; set; } // <photo>
}
public class Photo
{
int PhotoId { get; set; } // <pid>123</pid>
int AId { get; set; } // <aid>-7</aid>
int OwnerId { get; set; } // <owner_id>123</owner_id>
string Source { get; set; } // <src>http://url1.jpg</src>
string Source1Small { get; set; } // <src_small>http://url3.jpg</src_small>
string Source2Big { get; set; } // <src_big>http://url2.jpg</src_big>
string Source3XBig { get; set; } // <src_xbig>http://url4.jpg</src_xbig>
string Source4XXBig { get; set; } // <src_xxbig>http://url5.jpg</src_xxbig>
string Source5XXXBig { get; set; } // <src_xxxbig>http://url6.jpg</src_xxxbig>
int Width { get; set; } // <width>990</width>
int Height { get; set; } // <height>1188</height>
string Text { get; set; } // <text/>
DateTime CreatedDate { get; set; } // <created>135</created>
int AccessKey { get; set; } // <access_key>67</access_key>
}