[英]loop through specific attribute using xml linq and c# and get its lowest value
[英]How to find line number of specific value without its attribute using Linq in C#
我有 xml 文件,我想從沒有其屬性和父標簽的值中找到特定標簽的行號。
例如,我在第 321 行有地址標簽,我想使用 linq <address>Example1</address>
找到該行號,即使沒有它的父標簽和 xml 文件也沒有任何屬性。
這段代碼給了我沒有元素的例外。
var lineNumbers = xml.Descendants("ChildTag")
.Where(x =>!x.Descendants().Any() && //exact node contains the value
x.Value.Contains("value inside child tag"))
.Cast<IXmlLineInfo>()
.Select(x => x.LineNumber);
我已經檢查了堆棧溢出的所有解決方案,但沒有一個是完美的。 這里的確認是這里的價值。
注意:我想匹配該標簽內的值並獲取它的行號。
示例 XML 文件
<DocumentDetails>
<WitnessInfo>
<SROCode>342</SROCode>
<WitnessID>2324</WitnessID>
<DocumentID>158932420</DocumentID>
<Name>ExampleName</Name>
<Address>Address</Address>
<Age>23</Age>
<Profession />
<PhoneNo />
<IsOnline>false</IsOnline>
</WitnessInfo>
<PartyWitness>
<SROCode>2342</SROCode>
<PartyID>34234223</PartyID>
<WitnessID>342</WitnessID>
<WitnessDate>2022-10-03T11:59:52</WitnessDate>
</PartyWitness>
</DocumentDetails>
這里我有這個 xml 文件,那里有一個地址標簽,我從異常中獲取行號 321,我想找到它的父標簽。 但在找到父標簽之前,我想轉到第 321 行以匹配我從異常中獲得的行號。 那是我的問題。 我怎么才能拿到它..
您的數據不是有效的 XML ,因為字符 0x0C 不能出現在格式正確的 XML 中。 所以您不能使用任何 XML API 來加載它。
但是,您可以做的是通過將文件視為純文本文件來獲取有問題的行:
var lines = File.ReadAllLines(fileName);
var thisLine = lines[321];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.