我是xml的新手,需要使用类似于下面示例的xml循环遍历网页。我需要存储每个元素?将xml放入变量中,这样我就可以为每个唯一的id创建一个包含行的列表。 id:101,Description:Auto,Name:Bon Bon,Street:123 W Place St,State等。我相信id会属性?其余的值是innerxml?
我一直在网上搜索信息,但未能取得多大成就。任何帮助都会非常感激,因为我开始很慢。
<Master>
<Dealers>
<Dealer id="101">
<Description>Auto</Description>
<Name>Bon Bon Motors</Name>
<Address>
<Street>123 W Place St</Street>
<City>Chicago</City>
<State>IL</State>
<Zip>82453</Zip>
</Address>
<PhoneNo>5451252222</PhoneNo>
</Dealer>
<Dealer id-"102">
...
...
...
</Dealer>
</Dealers>
</Master>
XmlDocument doc = new XmlDocument();
doc.Load("http://data.xml");
XmlElement root = doc.DocumentElement;
List<SomeType> list1 = new List<SomeType>();
if (root.HasAttribute("id"))
{
foreach (.... )
{
int pk = root.Attributes["id"].Value;
string description = ...
string name ...
list1.Add(pk);
list1.Add(description);
....
}
}
答案 0 :(得分:3)
static void Main(string[] args)
{
var xml = @"<Master>
<Dealers>
<Dealer id=""101"">
<Description>Auto</Description>
<Name>Bon Bon Motors</Name>
<Address>
<Street>123 W Place St</Street>
<City>Chicago</City>
<State>IL</State>
<Zip>82453</Zip>
</Address>
<PhoneNo>5451252222</PhoneNo>
</Dealer>
<Dealer id=""102"">
<Description>Auto</Description>
<Name>Bon Bon Motors</Name>
<Address>
<Street>123 W Place St</Street>
<City>Chicago</City>
<State>IL</State>
<Zip>82453</Zip>
</Address>
<PhoneNo>5451252222</PhoneNo>
</Dealer>
<Dealer id=""103"">
<Description>Auto</Description>
<Name>Bon Bon Motors</Name>
<Address>
<Street>123 W Place St</Street>
<City>Chicago</City>
<State>IL</State>
<Zip>82453</Zip>
</Address>
<PhoneNo>5451252222</PhoneNo>
</Dealer>
</Dealers>
</Master>";
var results = XDocument.Parse(xml).Root // Master
.Descendants("Dealer")
.Select(dealer => new
{
Id = dealer.Attribute("id").Value,
Description = dealer.Element("Description").Value,
Name = dealer.Element("Name").Value,
Street = String.Join(", ", dealer.Element("Address")
.Elements() // Street, City, State, Zip
.Select(element => element.Value)
.ToArray())
}).ToList();
results.ForEach(result => Console.WriteLine("Id: {0}; Description: {1}; Name: {2}; Address: {3}",
result.Id, result.Description, result.Name, result.Street));
Console.Read();
}
打印到控制台:
Id: 101; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453
Id: 102; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453
Id: 103; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453
答案 1 :(得分:1)
这就像你想要的那样:
class Program
{
static void Main(string[] args)
{
var doc = new XmlDocument();
doc.Load(@"..\..\input.xml");
var container = doc.DocumentElement
.GetElementsByTagName("Dealers")
.OfType<XmlElement>()
.FirstOrDefault();
if (container == null) return;
var dealers = container
.GetElementsByTagName("Dealer")
.OfType<XmlElement>();
foreach (var dealer in dealers)
{
var dealerId = dealer.GetAttribute("id");
Console.Write(dealerId + " - ");
var descrip = dealer.GetElementsByTagName("Description").OfType<XmlElement>().FirstOrDefault();
if (descrip != null)
Console.WriteLine(descrip.InnerText);
// etc...
}
Console.ReadLine();
}
}
答案 2 :(得分:1)
这里有更多LINQified和简化方法。我刚刚写了它并测试了i var
var xmlDocument = XDocument.Load("C:\\TEMP\\test.xml");
var nodesList = from xmlNode in xmlDocument.Descendants("Dealer")
select
new
{
Id = xmlNode.Attribute("id").Value,
Description = xmlNode.Descendants("Description").ElementAt(0).Value,
Name = xmlNode.Descendants("Name").ElementAt(0).Value,
Address = new
{
Street = xmlNode.Descendants("Address").Descendants("Street").ElementAt(0).Value,
City = xmlNode.Descendants("Address").Descendants("City").ElementAt(0).Value,
State = xmlNode.Descendants("Address").Descendants("State").ElementAt(0).Value,
ZipCode = xmlNode.Descendants("Address").Descendants("Zip").ElementAt(0).Value
},
PhoneNumber = xmlNode.Descendants("PhoneNo").ElementAt(0).Value
}
;
foreach (var node in nodesList)
{
Console.WriteLine(node.Id);
}
Console.ReadKey();
答案 3 :(得分:0)
如果您了解XML文件的架构,则可以创建类并将XML反序列化为对象,然后您可以最大程度地灵活地处理它们。
在您的示例中,您有三个类,地址,经销商和主人。反序列化后,您将获得一个包含List属性的Master对象,每个Dealer对象都包含相关信息。