-1

I have xml file and I want to find line number of the specific tag from their value without its attribute and its parent tag.

For example, I have address tag on line number 321 and I want to find that line number using linq <address>Example1</address> even without its parent tag and xml file don't have any attribute..

This code is giving me exception that there is no element.

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);

I have checked all the solutions on stack overflow but nothing is working perfectly. Here acknowledgment is the value here.

Note: I want to match with the value inside that tag and get the line number of it.

Sample XML File

<DocumentDetails>
<WitnessInfo>
    <SROCode>342</SROCode>
    <WitnessID>2324</WitnessID>
    <DocumentID>158932420</DocumentID>
    <Name>ExampleName</Name>
    <Address>Address&#xC;</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>

Here I have this xml file and there is an address tag over there where I am getting line number 321 from the exception and I wan to find the parent tag of it. But before finding the parent tag I want to go to that line number 321 to matched with the line number which I got from the exception. That's my question. How I can fetch it..

Rushi
  • 1
  • 3
  • I think you could provide a sample xml file – MichaelMao Dec 23 '22 at 05:45
  • 1
    The code shown seem to do roughly what the question describes. Please double-check that you provided actual [mre], and make sure post contains sample XML as well, ideally as part of the code sample. While editing post with the data figure out if the problem is in the part where you select nodes or in getting line numbers (Note that line numbers are not always available (https://stackoverflow.com/questions/4471001/get-line-number-for-xelement-here).). – Alexei Levenkov Dec 23 '22 at 05:48
  • What do you want to do if the tag has [**mixed content**](https://www.w3schools.com/xml/schema_complex_mixed.asp), e.g. `some text bold more text`? What do you consider the "value inside the tag ``"? – dbc Dec 23 '22 at 05:49
  • It should matched with the value thats it. no I dont have mixed content. values are unique but tags are multiple. – Rushi Dec 23 '22 at 05:52
  • @AlexeiLevenkov - the problem may be that, for parent nodes, `XElement.Value` will contain the text content of all descendant nodes. To get just the text content of an `XElement` without text content of child elements, OP should see [this answer](https://stackoverflow.com/a/4251265) by [dtb](https://stackoverflow.com/a/4251265) to [How to get XElement's value and not value of all child-nodes?](https://stackoverflow.com/q/4251215). But, yes, a [mcve] would clear things up. – dbc Dec 23 '22 at 05:52
  • Works fine for me: [fiddle](https://dotnetfiddle.net/ZDwYWS) – Klaus Gütter Dec 23 '22 at 05:55
  • I want to load the document Not parse it.. – Rushi Dec 23 '22 at 06:04
  • Then replace Parse by Load in my example. – Klaus Gütter Dec 23 '22 at 06:08
  • Its giving me error can you check and give me the right solution – Rushi Dec 23 '22 at 06:20
  • If the only problem with your XML is that it has [characters that are not allowed by the XML standard](https://www.w3.org/TR/REC-xml/#NT-Char) and is otherwise well-formed, you can load your XML with `new XmlReaderSettings { CheckCharacters = false }` as shown in [this answer](https://stackoverflow.com/a/18020992/3744182) by [paulselles](https://stackoverflow.com/users/2646554/paulselles) to [XML Exception: Invalid Character(s)](https://stackoverflow.com/q/854335/3744182). – dbc Dec 23 '22 at 17:24
  • [C# how can I debug a deserialization exception?](https://stackoverflow.com/a/29882953/3744182) may also help you in finding the offending text content. – dbc Dec 23 '22 at 17:30

1 Answers1

1

Your data is no valid XML as the character 0x0C cannot occur in a well-formed XML. So you cannot use any of the XML APIs to load it (see also here: How to parse invalid (bad / not well-formed) XML?).

What you can however do is to get the offending line by treating the file as plain text file:

var lines = File.ReadAllLines(fileName);
var thisLine = lines[321-1];

Update: As was pointed out in the comments, the original code did not take into account that the line number from the exception is 1-based. Therefore the -1 above.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • 1
    Note that the line numbers from XML exceptions start at 1 not 0. A line number of 0 indicates that the XmlReader was unable to parse the first token. So if the exception occurred on "line number 321", querent should probably look at `lines[320]`. See: [C# how can I debug a deserialization exception?](https://stackoverflow.com/a/29882953/3744182). – dbc Dec 23 '22 at 17:20