我有一个带有命名空间的xml文件,我可以正确读取它。它有一个外部节点,名为' Items'它有多个子节点,其中有50个。(因此50个子节点称为' ReceiverPoints')当我检查控制台时,它的大小是正确的,即50但是当我检查打印输出时,所有的输出都是只重复第一个ReceiverPoint节点。
我想将每个接收点保存到数据库中。根据我看到的所有示例,我的实现看起来很好。但它给了我错误的结果。有人可以帮我看看我错过了什么吗?是xml file
public List<ReceiverPoint> ReadFile()
{
receiverList = new List<ReceiverPoint> ();
Console.WriteLine ("Now in read file method :" + fileLocation);
xmldoc = new XmlDocument ();
xmldoc.Load (fileLocation);
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager (xmldoc.NameTable);
//nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");
XmlElement rootElement = xmldoc.DocumentElement;
XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);
ReceiverPoint receiverPoint = new ReceiverPoint ();
foreach(XmlNode childNode in nodeList)
{
receiverPoint.CloseDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CloseDate", nameSpaceManager).InnerText;
receiverPoint.CreateDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateDate", nameSpaceManager).InnerText;
receiverPoint.CreateWho = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:CreateWho", nameSpaceManager).InnerText;
receiverPoint.Easting = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Easting", nameSpaceManager).InnerText;
receiverPoint.Elevation = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Elevation", nameSpaceManager).InnerText;
receiverPoint.IsDeployed = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsDeployed", nameSpaceManager).InnerText;
receiverPoint.IsManual = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:IsManual", nameSpaceManager).InnerText;
receiverPoint.LastModifyDate = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LastModifyDate", nameSpaceManager).InnerText;
receiverPoint.Latitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LatitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.Line = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Line", nameSpaceManager).InnerText;
receiverPoint.Longitude = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:LongitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.ReceiverType = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:ReceiverType", nameSpaceManager).InnerText;
receiverPoint.Station = childNode.SelectSingleNode ("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint/ns:Station", nameSpaceManager).InnerText;
//Get all the values stored in the receiver point object
string station = receiverPoint.Station;
string line = receiverPoint.Line;
string elevation = receiverPoint.Elevation;
string latitude = receiverPoint.Latitude;
string longitude = receiverPoint.Longitude;
string isDeployed = receiverPoint.IsDeployed;
string easting = receiverPoint.Easting;
string receiverType = receiverPoint.ReceiverType;
string closeDate = receiverPoint.CloseDate;
string createDate = receiverPoint.CreateDate;
string createWho = receiverPoint.CreateWho;
string lastModifyDate = receiverPoint.LastModifyDate;
Console.WriteLine ("String lat : " + latitude);
Console.WriteLine ("String lon : " + longitude);
Console.WriteLine ("String create date : " + createDate);
Console.WriteLine ("String create who : " + createWho);
//Save the data to the db
saveDataToDatabase (station,line,elevation,latitude,longitude,isDeployed,easting,receiverType,closeDate,createDate,createWho,lastModifyDate);
}
receiverList.Add (receiverPoint);
return receiverList;
}
答案 0 :(得分:1)
此代码适用于我:
public List<ReceiverPoint> ReadFile()
{
var receiverList = new List<ReceiverPoint>();
Console.WriteLine("Now in read file method :" + "");
var xmldoc = new XmlDocument();
xmldoc.Load(@"D:\users\..\Downloads\ReceiverPoints.xml");
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(xmldoc.NameTable);
//nameSpaceManager.AddNamespace ("ns", "http://schemas.datacontract.org/2004/07/AristotleService.Models");
nameSpaceManager.AddNamespace("ns", "http://schemas.datacontract.org/2004/07/GTI.Aristotle.Web.Api.Models");
XmlElement rootElement = xmldoc.DocumentElement;
XmlNodeList nodeList = rootElement.SelectNodes("/ns:PagedDataInquiryResponseOfReceiverPointQ3ffICf5/ns:Items/ns:ReceiverPoint", nameSpaceManager);
foreach (XmlNode childNode in nodeList)
{
ReceiverPoint receiverPoint = new ReceiverPoint();
receiverPoint.CloseDate = childNode.SelectSingleNode("ns:CloseDate", nameSpaceManager).InnerText;
receiverPoint.CreateDate = childNode.SelectSingleNode("ns:CreateDate", nameSpaceManager).InnerText;
receiverPoint.CreateWho = childNode.SelectSingleNode("ns:CreateWho", nameSpaceManager).InnerText;
receiverPoint.Easting = childNode.SelectSingleNode("ns:Easting", nameSpaceManager).InnerText;
receiverPoint.Elevation = childNode.SelectSingleNode("ns:Elevation", nameSpaceManager).InnerText;
receiverPoint.IsDeployed = childNode.SelectSingleNode("ns:IsDeployed", nameSpaceManager).InnerText;
receiverPoint.IsManual = childNode.SelectSingleNode("ns:IsManual", nameSpaceManager).InnerText;
receiverPoint.LastModifyDate = childNode.SelectSingleNode("ns:LastModifyDate", nameSpaceManager).InnerText;
receiverPoint.Latitude = childNode.SelectSingleNode("ns:LatitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.Line = childNode.SelectSingleNode("ns:Line", nameSpaceManager).InnerText;
receiverPoint.Longitude = childNode.SelectSingleNode("ns:LongitudeWGS84", nameSpaceManager).InnerText;
receiverPoint.ReceiverType = childNode.SelectSingleNode("ns:ReceiverType", nameSpaceManager).InnerText;
receiverPoint.Station = childNode.SelectSingleNode("ns:Station", nameSpaceManager).InnerText;
//Get all the values stored in the receiver point object
string station = receiverPoint.Station;
string line = receiverPoint.Line;
string elevation = receiverPoint.Elevation;
string latitude = receiverPoint.Latitude;
string longitude = receiverPoint.Longitude;
string isDeployed = receiverPoint.IsDeployed;
string easting = receiverPoint.Easting;
string receiverType = receiverPoint.ReceiverType;
string closeDate = receiverPoint.CloseDate;
string createDate = receiverPoint.CreateDate;
string createWho = receiverPoint.CreateWho;
string lastModifyDate = receiverPoint.LastModifyDate;
Console.WriteLine("String lat : " + latitude);
Console.WriteLine("String lon : " + longitude);
Console.WriteLine("String create date : " + createDate);
Console.WriteLine("String create who : " + createWho);
//Save the data to the db
//saveDataToDatabase(station, line, elevation, latitude, longitude, isDeployed, easting, receiverType, closeDate, createDate, createWho, lastModifyDate);
receiverList.Add(receiverPoint);
}
return receiverList;
}
答案 1 :(得分:0)
在foreach循环中移动receiverList.Add(receiverPoint)。