我有一个xml,我得到了回复,但有时一些属性没有来,所以我在解析时遇到异常因为我正在使用childnode我不知道如何解析使用属性可以任何人帮助。
xml是:
<?xml version="1.0"?>
<aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>2f63dc1c-3ce8-c3fa-0394-e9c1c31c4246</aws:RequestId></aws:OperationRequest><aws:UrlInfoResult>
<aws:Alexa>
<aws:ContactInfo>
<aws:DataUrl type="canonical">bose.com</aws:DataUrl>
<aws:PhoneNumbers>
<aws:PhoneNumber/>
</aws:PhoneNumbers>
<aws:OwnerName/>
<aws:Email/>
<aws:PhysicalAddress/>
<aws:CompanyStockTicker/>
</aws:ContactInfo>
<aws:ContentData>
<aws:DataUrl type="canonical">bose.com</aws:DataUrl>
<aws:SiteData>
<aws:Title>Bose</aws:Title>
<aws:Description>Manufacturers of complete home audio systems such as home theater systems, DVD systems and accessories.</aws:Description>
</aws:SiteData>
<aws:LinksInCount>2368</aws:LinksInCount>
<aws:Keywords>
<aws:Keyword>Consumer Electronics</aws:Keyword>
</aws:Keywords>
</aws:ContentData>
<aws:Related>
<aws:DataUrl type="canonical">bose.com</aws:DataUrl>
<aws:Categories>
<aws:CategoryData>
<aws:Title>Electronics/Audio</aws:Title>
<aws:AbsolutePath>Top/Business/Consumer_Goods_and_Services/Electronics/Audio</aws:AbsolutePath>
</aws:CategoryData>
<aws:CategoryData>
<aws:Title>Shopping/Consumer Electronics</aws:Title>
<aws:AbsolutePath>Top/Shopping/Consumer_Electronics/B</aws:AbsolutePath>
</aws:CategoryData>
<aws:CategoryData>
<aws:Title>Business and Economy/Entertainment and Media</aws:Title>
<aws:AbsolutePath>Top/Regional/North_America/United_States/Massachusetts/Localities/F/Framingham/Business_and_Economy/Entertainment_and_Media</aws:AbsolutePath>
</aws:CategoryData>
</aws:Categories>
</aws:Related>
<aws:TrafficData>
<aws:DataUrl type="canonical">bose.com</aws:DataUrl>
<aws:Rank>11305</aws:Rank>
<aws:UsageStatistics>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Months>3</aws:Months>
</aws:TimeRange>
<aws:Rank>
<aws:Value>11305</aws:Value>
<aws:Delta>+1450</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>11392</aws:Value>
<aws:Delta>+1594</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>87.6</aws:Value>
<aws:Delta>-13.94%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>5.9</aws:Value>
<aws:Delta>-11.85%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>13240</aws:Value>
<aws:Delta>1318</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>2.87</aws:Value>
<aws:Delta>+3%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Rank>
<aws:Value>11396</aws:Value>
<aws:Delta>+175</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>11602</aws:Value>
<aws:Delta>+176</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>86.9</aws:Value>
<aws:Delta>-0.510%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>5.93</aws:Value>
<aws:Delta>-1.76%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>13221</aws:Value>
<aws:Delta>331</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>2.92</aws:Value>
<aws:Delta>-1.02%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Days>7</aws:Days>
</aws:TimeRange>
<aws:Rank>
<aws:Value>11570</aws:Value>
<aws:Delta>+394</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>11949</aws:Value>
<aws:Delta>+569</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>84</aws:Value>
<aws:Delta>-5.640%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>5.99</aws:Value>
<aws:Delta>-0.41%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>12951</aws:Value>
<aws:Delta>-102</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>3.0</aws:Value>
<aws:Delta>+6%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
<aws:UsageStatistic>
<aws:TimeRange>
<aws:Days>1</aws:Days>
</aws:TimeRange>
<aws:Rank>
<aws:Value>11837</aws:Value>
<aws:Delta>-215</aws:Delta>
</aws:Rank>
<aws:Reach>
<aws:Rank>
<aws:Value>12937</aws:Value>
<aws:Delta>-21</aws:Delta>
</aws:Rank>
<aws:PerMillion>
<aws:Value>78</aws:Value>
<aws:Delta>-0.40%</aws:Delta>
</aws:PerMillion>
</aws:Reach>
<aws:PageViews>
<aws:PerMillion>
<aws:Value>6.7</aws:Value>
<aws:Delta>+7%</aws:Delta>
</aws:PerMillion>
<aws:Rank>
<aws:Value>11807</aws:Value>
<aws:Delta>-897</aws:Delta>
</aws:Rank>
<aws:PerUser>
<aws:Value>3.8</aws:Value>
<aws:Delta>+7%</aws:Delta>
</aws:PerUser>
</aws:PageViews>
</aws:UsageStatistic>
</aws:UsageStatistics>
<aws:ContributingSubdomains>
<aws:ContributingSubdomain>
<aws:DataUrl>bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>79.41%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>74.98%</aws:Percentage>
<aws:PerUser>2.75</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>community.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>12.41%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>9.10%</aws:Percentage>
<aws:PerUser>2.1</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>global.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>6.49%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>3.08%</aws:Percentage>
<aws:PerUser>1.4</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>pro.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>2.32%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>2.43%</aws:Percentage>
<aws:PerUser>3.0</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>assets.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>4.01%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>1.89%</aws:Percentage>
<aws:PerUser>1.4</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>btu.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>4.24%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>1.75%</aws:Percentage>
<aws:PerUser>1.2</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>accommodations.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.37%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.93%</aws:Percentage>
<aws:PerUser>7.3</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>downloads.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>2.54%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.90%</aws:Percentage>
<aws:PerUser>1.0</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>knowledgebase.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.24%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.74%</aws:Percentage>
<aws:PerUser>9.1</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>gd.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.29%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.66%</aws:Percentage>
<aws:PerUser>6.6</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>em.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.69%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.61%</aws:Percentage>
<aws:PerUser>2.6</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>globalpressroom.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.50%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.58%</aws:Percentage>
<aws:PerUser>3.3</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>hearphones.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.95%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.42%</aws:Percentage>
<aws:PerUser>1.3</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>reseller.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.21%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.39%</aws:Percentage>
<aws:PerUser>5</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>itsm.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.17%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.25%</aws:Percentage>
<aws:PerUser>4</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>auth.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.36%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.21%</aws:Percentage>
<aws:PerUser>1.7</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>build.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.40%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.17%</aws:Percentage>
<aws:PerUser>1.2</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>products.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.43%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.17%</aws:Percentage>
<aws:PerUser>1.1</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>employeepurchase.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.07%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.16%</aws:Percentage>
<aws:PerUser>7</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>professional.bose.com</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0.23%</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.10%</aws:Percentage>
<aws:PerUser>1</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
<aws:ContributingSubdomain>
<aws:DataUrl>OTHER</aws:DataUrl>
<aws:TimeRange>
<aws:Months>1</aws:Months>
</aws:TimeRange>
<aws:Reach>
<aws:Percentage>0</aws:Percentage>
</aws:Reach>
<aws:PageViews>
<aws:Percentage>0.49%</aws:Percentage>
<aws:PerUser>0</aws:PerUser>
</aws:PageViews>
</aws:ContributingSubdomain>
</aws:ContributingSubdomains>
</aws:TrafficData>
</aws:Alexa></aws:UrlInfoResult><aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:StatusCode>Success</aws:StatusCode></aws:ResponseStatus>
</aws:Response></aws:UrlInfoResponse>
答案 0 :(得分:1)
试试xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication3
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
public static void Main(string[] args)
{
Response response = new Response(FILENAME);
}
}
public class Response
{
public Response(string filename)
{
XDocument doc = XDocument.Load(filename);
XElement response = doc.Descendants().Where(x => x.Name.LocalName == "Response").FirstOrDefault();
XNamespace awsNs = response.GetNamespaceOfPrefix("aws");
requestId = (string)response.Descendants(awsNs + "RequestId").FirstOrDefault();
XElement alexa = response.Descendants(awsNs + "Alexa").FirstOrDefault();
contentData = alexa.Elements(awsNs + "ContentData").Select(x => new ContentData() {
urlType = (string)x.Element(awsNs + "DataUrl").Attribute("type"),
url = (string)x.Element(awsNs + "DataUrl"),
title = (string)x.Descendants(awsNs + "Title").FirstOrDefault(),
description = (string)x.Descendants(awsNs + "Description").FirstOrDefault(),
linkCount = (int)x.Element(awsNs + "LinksInCount"),
keyWords = x.Elements(awsNs + "Keywords").Select(y => (string)y).ToList()
}).FirstOrDefault();
categoryData = alexa.Element(awsNs + "Related").Descendants(awsNs + "CategoryData")
.GroupBy(x => (string)x.Element(awsNs + "Title"), y => (string)y.Element(awsNs + "AbsolutePath"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
trafficData = alexa.Elements(awsNs + "TrafficData").Select(x => new TrafficData() {
rank = (int)x.Element(awsNs + "Rank"),
statics = x.Descendants(awsNs + "UsageStatistic").Select(y => new Statistics() {
months = (int?)y.Descendants(awsNs + "Months").FirstOrDefault(),
days = (int?)y.Descendants(awsNs + "Days").FirstOrDefault(),
rankValue = (int)y.Element(awsNs + "Rank").Element(awsNs + "Value"),
rankDelta = (int)y.Element(awsNs + "Rank").Element(awsNs + "Delta"),
reachrankValue = (int)y.Element(awsNs + "Reach").Element(awsNs + "Rank").Element(awsNs + "Value"),
reachRankDelta = (int)y.Element(awsNs + "Reach").Element(awsNs + "Rank").Element(awsNs + "Value"),
reachPerMillionValue = (decimal)y.Element(awsNs + "Reach").Element(awsNs + "PerMillion").Element(awsNs + "Value"),
reachPerMillionDeltaPercentage = decimal.Parse(((string)(y.Element(awsNs + "Reach").Element(awsNs + "PerMillion").Element(awsNs + "Delta"))).Split(new char[] {'%'}).FirstOrDefault()),
pageViewsPerMillionValue = (decimal)y.Element(awsNs + "PageViews").Element(awsNs + "PerMillion").Element(awsNs + "Value"),
pageViewsPerMillionDeltaPercentage = decimal.Parse(((string)(y.Element(awsNs + "PageViews").Element(awsNs + "PerMillion").Element(awsNs + "Delta"))).Split(new char[] {'%'}).FirstOrDefault()),
pageViewsRankValue = (int)y.Element(awsNs + "PageViews").Element(awsNs + "Rank").Element(awsNs + "Value"),
pageViewsRankDelta = (int)y.Element(awsNs + "PageViews").Element(awsNs + "Rank").Element(awsNs + "Value"),
pageViewsPerUserValue = (decimal)y.Element(awsNs + "PageViews").Element(awsNs + "PerUser").Element(awsNs + "Value"),
pageViewsPerUserDeltaPercentage = decimal.Parse(((string)(y.Element(awsNs + "PageViews").Element(awsNs + "PerUser").Element(awsNs + "Delta"))).Split(new char[] {'%'}).FirstOrDefault())
}).ToList(),
contributingSubdomain = x.Descendants(awsNs + "ContributingSubdomain").Select(y => new ContributingSubdomain() {
dataUrl = (string)y.Element(awsNs + "DataUrl"),
months = (int?)y.Descendants(awsNs + "Months").FirstOrDefault(),
days = (int?)y.Descendants(awsNs + "Days").FirstOrDefault(),
reachPercentage = decimal.Parse(((string)(y.Element(awsNs + "Reach").Element(awsNs + "Percentage"))).Split(new char[] { '%' }).FirstOrDefault()),
pageViewsPercentage = decimal.Parse(((string)(y.Element(awsNs + "PageViews").Element(awsNs + "Percentage"))).Split(new char[] { '%' }).FirstOrDefault()),
pageViewsPerUser = (decimal)y.Element(awsNs + "PageViews").Element(awsNs + "PerUser")
}).ToList()
}).FirstOrDefault();
}
public string requestId { get; set; }
public ContentData contentData { get; set;}
public Dictionary<string, string> categoryData { get; set; }
public TrafficData trafficData { get; set; }
}
public class ContentData
{
public string urlType {get;set;}
public string url { get; set;}
public string title { get; set;}
public string description { get; set;}
public int linkCount { get;set;}
public List<string> keyWords { get; set;}
}
public class TrafficData
{
public int rank { get;set;}
public List<Statistics> statics { get; set;}
public List<ContributingSubdomain> contributingSubdomain { get; set; }
}
public class Statistics
{
public int? days { get; set; }
public int? months { get; set; }
public int rankValue { get; set;}
public int rankDelta { get;set;}
public int reachrankValue { get; set; }
public int reachRankDelta { get; set; }
public decimal reachPerMillionValue { get; set; }
public decimal reachPerMillionDeltaPercentage { get; set; }
public decimal pageViewsPerMillionValue { get; set; }
public decimal pageViewsPerMillionDeltaPercentage { get; set; }
public decimal pageViewsRankValue { get; set; }
public decimal pageViewsRankDelta { get; set; }
public decimal pageViewsPerUserValue { get; set; }
public decimal pageViewsPerUserDeltaPercentage { get; set; }
}
public class ContributingSubdomain
{
public string dataUrl { get; set; }
public int? months { get; set; }
public int? days { get; set; }
public decimal reachPercentage { get; set; }
public decimal pageViewsPercentage { get; set; }
public decimal pageViewsPerUser { get; set; }
}
}
答案 1 :(得分:0)
我建议您使用XPath而不是按索引引用节点。
E.g。 XPath获取URL(未经测试的代码):
var url = node.SelectSingleNode("//aws2:ContactInfo/aws2:DataUrl/text());
而不是:
if(node.ChildNodes[i].Name== "aws:ContactInfo")
{
merchwebsite.WebsiteName=node.ChildNodes[i].ChildNodes[0].InnerText;
}