C# 从XML文件加载重复的元素
我希望从一个名为C# 从XML文件加载重复的元素,c#,xml,C#,Xml,我希望从一个名为initialInspections.XML的XML文件中加载重复的元素。问题在于系统需要是动态的,并允许添加尽可能多的不同的检查注释。我需要输入所有它们,即使它们具有相同的名称 如果有人能给我一个这样做的方法,我会非常感激,因为我已经搜索了近3个小时了,我还没有找到任何有效的 我需要从每个inspectionNote节点中删除所有数据,并将其放入名为initialInspectionNotes的结构数组中 以下是我到目前为止的情况: public int propert
initialInspections.XML
的XML文件中加载重复的元素。问题在于系统需要是动态的,并允许添加尽可能多的不同的检查注释。我需要输入所有它们,即使它们具有相同的名称
如果有人能给我一个这样做的方法,我会非常感激,因为我已经搜索了近3个小时了,我还没有找到任何有效的
我需要从每个inspectionNote
节点中删除所有数据,并将其放入名为initialInspectionNotes
的结构数组中
以下是我到目前为止的情况:
public int propertyID;
public string initialInspectorUsername;
public DateTime intialDateTime;
public struct initialInspectionNotes
{
public string locationWithinProperty;
public string locationExtraNote;
public string costCode;
public float estimatedTime;
}
private void finalInspection_Load(object sender, EventArgs e)
{
//Open the intialInspections xml file and load the values into the form
XmlDocument xdoc = new XmlDocument();
FileStream rFile = new FileStream(values.xmlInitialFileLocation, FileMode.Open);
xdoc.Load(rFile);
XmlNodeList list = xdoc.GetElementsByTagName("initialInspection");
for (int i = 0; i < list.Count; i++)
{
XmlElement initialInspection = (XmlElement)xdoc.GetElementsByTagName("initialInspection")[i];
XmlElement initialInspector = (XmlElement)xdoc.GetElementsByTagName("userInspection")[i];
XmlElement dateTime = (XmlElement)xdoc.GetElementsByTagName("dateTime")[i];
propertyID = int.Parse(initialInspection.GetAttribute("propertyID"));
initialInspectorUsername = initialInspector.InnerText;
intialDateTime = DateTime.Parse(dateTime.InnerText);
}
rFile.Close();
}
public int propertyID;
公共字符串initialInspectorUsername;
公共日期时间intialDateTime;
公共结构initialInspectionNotes
{
属性中的公共字符串位置;
公共字符串位置Extranote;
公共字符串代价码;
公共浮动估计时间;
}
私有void finalInspection\u加载(对象发送方,事件参数e)
{
//打开intialInspections xml文件并将值加载到表单中
XmlDocument xdoc=新的XmlDocument();
FileStream rFile=newfilestream(values.xmlInitialFileLocation,FileMode.Open);
xdoc.Load(rFile);
XmlNodeList=xdoc.GetElementsByTagName(“初始检查”);
for(int i=0;i
XML如下所示:
<?xml version="1.0" standalone="yes"?>
<initialInspections>
<initialInspection propertyID="1">
<userInspection>defaultadmin</userInspection>
<dateTime>07/11/2015 17:15:20</dateTime>
<inspectionNote>
<location>Dining Room</location>
<locationNote>Remove whole carpet, leave underlay</locationNote>
<CostCode>L1</CostCode>
<estimatedTime>5</estimatedTime>
</inspectionNote>
<inspectionNote>
<location>Other - See Notes</location>
<locationNote>On the marked area with orange spray paint.</locationNote>
<CostCode>B1</CostCode>
<estimatedTime>12</estimatedTime>
</inspectionNote>
</initialInspection>
</initialInspections>
默认管理员
07/11/2015 17:15:20
餐厅
除去整个地毯,留下衬垫
L1
5.
其他-见附注
在标记区域喷涂橙色喷漆。
地下一层
12
任何帮助都将不胜感激。一种可能是使用和方法一次获取所有检查注释
:
var xml = XDocument.Load(fileLocation); // for example c:\temp\input.xml
// fetch all inspectionNotes
var inspectionNotes = xml.Root.Descendants("inspectionNote").ToList();
// TODO: error handling!
// map inspectionNote node to custom structure
var arrayOfNotes = inspectionNotes.Select (n => new initialInspectionNotes
{
costCode = n.Element("CostCode").Value,
estimatedTime = float.Parse(n.Element("estimatedTime").Value),
locationExtraNote = n.Element("locationNote").Value,
locationWithinProperty = n.Element("location").Value,
})
// and convert the result to array holding elements of the custom structure
.ToArray();
foreach (var note in arrayOfNotes)
{
Console.WriteLine(note.locationExtraNote);
}
输出为:
Remove whole carpet, leave underlay
On the marked area with orange spray paint.
如果您想读取和映射其他XML节点(例如,initialInspection
),同样的逻辑也适用
如果需要使用XmlReader
,则使用和获取内部inspectionNote
元素以及每个inspectionNote
元素的值:
//打开intialInspections xml文件并将值加载到表单中
XmlDocument xdoc=新的XmlDocument();
FileStream rFile=newfilestream(values.xmlInitialFileLocation,FileMode.Open);
xdoc.Load(rFile);
XmlNodeList=xdoc.GetElementsByTagName(“初始检查”);
//创建initialInspectionNotes列表,以便根据需要添加尽可能多的节点
var notes=新列表();
//地图数据
for(int i=0;i
无论XML包含多少inspectionNote
元素,列表都会显示。数组将读取所有注释。一种可能是使用和方法一次获取所有检查注释
:
var xml = XDocument.Load(fileLocation); // for example c:\temp\input.xml
// fetch all inspectionNotes
var inspectionNotes = xml.Root.Descendants("inspectionNote").ToList();
// TODO: error handling!
// map inspectionNote node to custom structure
var arrayOfNotes = inspectionNotes.Select (n => new initialInspectionNotes
{
costCode = n.Element("CostCode").Value,
estimatedTime = float.Parse(n.Element("estimatedTime").Value),
locationExtraNote = n.Element("locationNote").Value,
locationWithinProperty = n.Element("location").Value,
})
// and convert the result to array holding elements of the custom structure
.ToArray();
foreach (var note in arrayOfNotes)
{
Console.WriteLine(note.locationExtraNote);
}
class Note
{
public string Location { get; set; }
public string LocationNote { get; set; }
public string CodeCost { get; set; }
public string EstimatedTime { get; set; }
}
var xml = XElement.Load(...your xml path here );
var data = xml.Descendants("initialInspection").Elements("inspectionNote").Select(n => new Note()
{
Location = n.Element("location").Value,
LocationNote = n.Element("locationNote").Value,
CodeCost = n.Element("CostCode").Value,
EstimatedTime = n.Element("estimatedTime").Value
}).ToList();
输出为:
Remove whole carpet, leave underlay
On the marked area with orange spray paint.
如果您想读取和映射其他XML节点(例如,initialInspection
),同样的逻辑也适用
如果需要使用XmlReader
,则使用和获取内部inspectionNote
元素以及每个inspectionNote
元素的值:
//打开intialInspections xml文件并将值加载到表单中
XmlDocument xdoc=新的XmlDocument();
FileStream rFile=newfilestream(values.xmlInitialFileLocation,FileMode.Open);
xdoc.Load(rFile);
XmlNodeList=xdoc.GetElementsByTagName(“初始检查”);
//创建initialInspectionNotes列表,以便根据需要添加尽可能多的节点
var notes=新列表();
//地图数据
for(int i=0;ivar dataNodes = document.GetElementsByTagName("Data");
var toList = dataNodes.OfType<XmlElement>().ToList();