[英]Reading XML Nodes
我有以下XML文件。 我正在嘗試讀取dept元素的startDate節點,但不想讀取其余所有子元素(如“ DeptRoles”)的“ startDate”節點。
<dept operationalStatus="active" primaryRole="Admin" depChangeDate="20130420">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>
<DeptRoles>
<DeptRole name="Other dept" status="active">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"/>
<isPrimary/>
</DeptRole>
</DeptRoles>
</dept>
這是我的C#代碼。 這段代碼也得到了DeptRole startDate元素,我不想。
public static List<organisation> Getorgdata(string data)
{
List<organisation> listorgdata = new List<organisation>();
XmlReader xmlReader = XmlReader.Create(new StringReader(data));
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
organisation record = new organisation();
if (xmlReader.HasAttributes && xmlReader.Name == "dept")
{
record.orgOperationalStatus = xmlReader.GetAttribute("operationalStatus");
record.orgLegalStatus = xmlReader.GetAttribute("legalStatus");
}
else if (xmlReader.Name == "name")
{
record.orgName = xmlReader.ReadElementString("name");
}
else if (xmlReader.Name == "startDate" || xmlReader.Name == "endDate")
{
if (xmlReader.GetAttribute("type") == "legal")
{
record.orgLegalStartDate = xmlReader.ReadElementString("startDate");
record.orgLegalEndDate = xmlReader.ReadElementString("endDate");
}
else if (xmlReader.GetAttribute("type") == "operational")
{
record.orgOperationalStartDate = xmlReader.ReadElementString("startDate");
record.orgOperationalEndDate = xmlReader.ReadElementString("endDate");
listorgdata.Add(record);
}
}
}
}
return listorgdata;
}
我將使用LINQ to XML(正如其他人所建議的)。 以下是與您發布的代碼有些匹配的示例代碼-代碼說明如下。
public static List<organisation> Getorgdata(string data)
{
List<organisation> listorgdata = new List<organisation>();
XDocument xDoc = XDocument.Parse(data);
listorgdata = xDoc.Root.Elements("dept")
.Select(x => new organisation()
{
orgOperationalStatus = (string)x.Attribute("operationalStatus"),
orgLegalStartDate = (string)x.Elements("startDate")
.Where(x1 => (string)x1.Attribute("type") == "legal")
.First(),
orgOperationalStartDate = (string)x.Elements("startDate")
.Where(x1 => (string)x1.Attribute("type") == "operational")
.First()
}).ToList();
return listorgdata;
}
上面的代碼要做的第一件事是使用XDocument.Parse
方法將XML加載到XDocument.Parse
。
然后,查詢將解析XML文檔並返回已填充的organization
對象的列表。 它像這樣。
<dept>
元素(和子元素)。 <dept>
元素,創建的新實例organisation
和分配“OperationalStatus的”屬性到的值operationalStatus
財產organisation
。 (string)
強制轉換將很好地處理指定屬性不存在的情況。 <dept>
元素的<startDate>
子元素進行查詢,並獲取第一個屬性為“ type”等於“ legal”的元素。 .ToList()
擴展方法,將IEnumerabale<T>
結果轉換為列表。 這可能不是最有效的方法,並且不能涵蓋原始代碼中的所有屬性,但至少可以使您朝正確的方向前進。 例如,如果要獲取“ primaryRole”屬性,只需在new organisation()
塊中添加以下行:
orgPrimaryRole = (string)x.Attribute("primaryRole"),
如果您需要來自<DeptRoles>
的數據,則可以使用它,但是要涉及更多的內容。
如果您更喜歡查詢語法而不是方法語法,它將看起來像這樣:
listorgdata = (from x in xDoc.Root.Elements("dept")
select new
{
orgOperationalStatus = (string)x.Attribute("operationalStatus"),
orgLegalStartDate = (from x1 in x.Elements("startDate")
where (string)x1.Attribute("type") == "legal"
select (string)x1).First(),
orgOperationalStartDate = (from x1 in x.Elements("startDate")
where (string)x1.Attribute("type") == "operational"
select (string)x1).First()
}).ToList();
如果我添加目錄
<CATALOG>
<dept operationalStatus="active" primaryRole="Admin" depChangeDate="20130420">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"></endDate>
<DeptRoles>
<DeptRole name="Other dept" status="active">
<startDate type="legal">20130401</startDate>
<endDate type="legal"></endDate>
<startDate type="operational">20130320</startDate>
<endDate type="operational"/>
<isPrimary/>
</DeptRole>
</DeptRoles>
</dept>
</CATALOG>
通過使用System.Xml.Linq; 你可以得到你的清單:
XElement myXML = XElement.Load(@"C:\Users\User\Desktop\myXML.xml");
var deptDescendantsList = (from ep in myXML.Descendants("dept")
select ep.Elements("startDate")).ToList();
我不確定如何創建完全適合您需求的查詢。 嘗試將此查詢追加到您的需求。 http://msdn.microsoft.com/zh-cn/library/bb387061.aspx包含有關linq2xml查詢的一些信息
XmlReader xmlReader = XmlReader.Create(new StringReader(data));
XDocument d = XDocument.Load(xmlReader);
var startDates = from item in d.Root.Descendants()
where item.Name == "startDate" && item.Parent.Name == "dept" && item.Parent.Attribute("deptid").Value == "anynumber"
select item;
// iterate over the startDates and do your magic
嘗試這個 :
var readSpecific =來自xd.Descendants(“ departments”)中的a
select new
{
LegalStartDate = a.Element("dept").Elements("startDate").First(cc => cc.Attribute("type").Value == "legal").Value,
LegalEndDate = a.Element("dept").Elements("endDate").First(cc => cc.Attribute("type").Value == "legal").Value,
OperationalStartDate = a.Element("dept").Elements("startDate").First(cc => cc.Attribute("type").Value == "operational").Value,
OperationEndDate = a.Elements("dept").Elements("endDate").First(cc => cc.Attribute("type").Value == "operational").Value
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.