简体   繁体   中英

How to find nodes in XML File using Line number C#?

I have below XML files. While I'm using it in Process its gives me error like

[2016-06-22 19:29:53 IST] ERROR: "Line 15 column 20: character content of element "electronics" invalid; must be equal to "foods", "goods" or "mobiles" at XPath /products/product[1]/item_type"

This XPath /products/product[1] is not correct. it's returns by their system. So, I have to search it and replace with original value through Line No only.

Order.xml

<products>
    <product>
        <country>AE</country>
        <sale>false</sale>
        <item_type>foods</item_type>
    </product>
    <product>
        <country>IN</country>
        <sale>false</sale>
        <item_type>goods</item_type>
    </product>
    <product>
        <country>US</country>
        <sale>false</sale>
        <item_type>electronics</item_type>
    </product>
    <product>
        <country>AM</country>
        <sale>false</sale>
        <item_type>mobiles</item_type>
    </product>
</products>

Please let me know to do search using Line No(15) in XML document

You should not rely on line/colmns numbers when dealing with xml. Xml does not care much for whitespace and formatting is a merely convenience for readabilty; Although it is correctly reported in your example, I would not trust it will alway be that way in future. Interstingly the xpath is also incorrect but lets not dwell on that for the moment.

You need to take back control of the problem... You should create your own validating reader and capture the validation error while the xml is being parsed. How you do this will depend on which framework your using. Either XmlValidatingReader or, XmlReader with the appropriate XmlReaderSettings. You can set up a callback/eventhandler to capture the error as the xml file is being read, so you can be certain your in the correct place and have all the information you need to handle the error to hand. Using an XmlReader will also allow you to continue to process the entire file and not just stop at the first error.

The code is too big for SO and we'd require a lot more information from you to do it but a google search will find lots of examples including this one from microsoft: https://msdn.microsoft.com/en-us/library/w5aahf2a(v=vs.100).aspx

  1. Construct a new XmlReaderSettings instance.

  2. Add an XML schema to the Schemas property of the XmlReaderSettings instance.

  3. Specify Schema as the ValidationType.

  4. Specify ValidationFlags and a ValidationEventHandler to handle schema validation errors and warnings encountered during validation.

  5. Pass the XmlReaderSettings object to the Create method of the XmlReader class along with the XML document, creating a schema-validating XmlReader.

  6. Call Read() to run through the xml from start to end.

Would this work for you?

string[] lines = File.ReadAllLines(@"C:\order.xml", Encoding.UTF8);
var line15 = lines[14];
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\order.xml");
XmlElement el =(XmlElement)doc.SelectSingleNode("/products/product[1]/item_type");
if(el != null) { el.ParentNode.RemoveChild(el); }

you can remove node by using above code...using proper Xpath you can remove all the item_type which is not equals to foods,good or mobiles.

Try this,

var xml = XDocument.Load(@"C:\order.xml", LoadOptions.SetLineInfo);
var lineNumbers = xml.Descendants()
                     .Where(x => !x.Descendants().Any() && //exact node contains the value
                                  x.Value.Contains("foods"))
                     .Cast<IXmlLineInfo>()
                     .Select(x => x.LineNumber);
int getLineNumber = lineNumbers.First();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM