我有一個包含 Location 元素的 XML 檔案,它的所有子元素都包含我需要放入串列中的資訊,或者理想情況下放入 Location 型別物件中。
在這里,我定義了我的位置類:
public class Location
{
/* <Summary>
* Class that describes a location object in GPL. This class is
* used to get/put location objects from TCS
*/
#region Private Declarations
private string _msg;
private string _msgIn;
private string[] _msgRes;
private TcpIpComm _client { get; set; }
#endregion
public Location(TcpIpComm commObj, string locName)
{
_client = commObj;
Name = locName;
}
public Location(TcpIpComm commObj)
{
_client = commObj;
}
public Location()
{
}
public string Name { get; set; }
public int Index { get; set; }
public float Joint1 { get; set; }
public float Joint2 { get; set; }
public float Joint3 { get; set; }
public float Joint4 { get; set; }
public float Joint5 { get; set; }
public float Joint6 { get; set; }
public float Joint7 { get; set; }
public float ZClearance = 0;
public LocType Type { get; set; }
/// <summary>
/// 0 = cartesian,
/// 1 = angles
/// </summary>
public enum LocType
{
Cartesian = 0,
Angles = 1
}
}
這是我的 XML 檔案的示例:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLocations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Location>
<Name>LocName0</Name>
<Type>Angles</Type>
<Index>0</Index>
<ZClearance>0</ZClearance>
<Joint1>0</Joint1>
<Joint2>0</Joint2>
<Joint3>0</Joint3>
<Joint4>0</Joint4>
<Joint5>0</Joint5>
<joint6>0</joint6>
<Joint6>0</Joint6>
</Location>
<Location>
<Name>LocName1</Name>
<Type>Angles</Type>
<Index>0</Index>
<ZClearance>0</ZClearance>
<Joint1>1</Joint1>
<Joint2>1</Joint2>
<Joint3>1</Joint3>
<Joint4>1</Joint4>
<Joint5>1</Joint5>
<joint6>1</joint6>
<Joint6>1</Joint6>
</Location>
<Location>
<Name>LocName2</Name>
<Type>Angles</Type>
<Index>0</Index>
<ZClearance>0</ZClearance>
<Joint1>2</Joint1>
<Joint2>2</Joint2>
<Joint3>2</Joint3>
<Joint4>2</Joint4>
<Joint5>2</Joint5>
<joint6>2</joint6>
<Joint6>2</Joint6>
</Location>
現在我希望能夠在方法中輸入位置名稱(例如:LocName0),并將該名稱元素下的所有值在串列中回傳給我(型別、索引、ZClearance、Joint1、Joint2 等... )。我正在努力解決這個問題,因為 Name 元素不是任何東西的父元素。它是 Location 的子元素,因此 Name 下面的 Elements 不是后代,AFAIK。
這是我寫的一種方法,試圖做我所描述的:
public static void GetTeachPoint(string teachName, string filePath)
{
var listOfElems = XElement.Parse(filePath)
.Elements() // gets all elements under root
.Where(x => x.Name.LocalName.StartsWith(teachName))
.SelectMany(x => x.Elements())
.Select(x => x.Value)
.ToList();
foreach(string item in listOfElems){
MessageBox.Show(item);
}
}
但是,當我將位置名稱和檔案路徑傳遞給方法時,我收到一個
System.Xml.XmlException: 'Data at the root level is invalid. Line 1, position 1.'
uj5u.com熱心網友回復:
這不會像您認為的那樣做:
XElement.Parse(filePath)
這試圖決議filePath
好像它實際上是 XML。我懷疑您打算使用XElement.Load
which 從檔案加載 XML - 或者您可能使用XDocument.Load
.
這將克服最初的問題,但您的查詢仍然被破壞。你想要(我相信)類似的東西:
var elements = XElement.Load(filePath)
.Elements("Location")
.Elements("Name")
.Where(x => x.Value == teachName)
.SelectMany(x => x.Parent.Elements());
uj5u.com熱心網友回復:
嘗試以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfLocations));
ArrayOfLocations location = (ArrayOfLocations)serializer.Deserialize(reader);
Dictionary<string, Location> dict = location.location
.GroupBy(x => x.Name)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
public class ArrayOfLocations
{
[XmlElement("Location")]
public List<Location> location { get; set; }
}
public class Location
{
private string _msg;
private string _msgIn;
private string[] _msgRes;
public Location()
{
}
public string Name { get; set; }
public int Index { get; set; }
public float Joint1 { get; set; }
public float Joint2 { get; set; }
public float Joint3 { get; set; }
public float Joint4 { get; set; }
public float Joint5 { get; set; }
public float Joint6 { get; set; }
public float Joint7 { get; set; }
public float ZClearance = 0;
public LocType Type { get; set; }
/// <summary>
/// 0 = cartesian,
/// 1 = angles
/// </summary>
public enum LocType
{
Cartesian = 0,
Angles = 1
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/515633.html
標籤:C#xml林克
上一篇:C#讀取父ID下的特定節點