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
Construct a new XmlReaderSettings instance.
Add an XML schema to the Schemas property of the XmlReaderSettings instance.
Specify Schema as the ValidationType.
Specify ValidationFlags and a ValidationEventHandler to handle schema validation errors and warnings encountered during validation.
Pass the XmlReaderSettings object to the Create method of the XmlReader class along with the XML document, creating a schema-validating XmlReader.
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();
Related
My tasks seems to sound simple but is proving very difficult. I have a XML file like below:
<?xml version="1.0" encoding="UTF-8"?>
<ref:ReferralDocument xmlns:ref="http://ref.com" xmlns:gen="http://ref.com"
xmlns:h="http://www.w3.org/1999/xhtml" xmlns:pbr="ref.com"
xmlns:xsi="ref.com" schemaVersion="2.9">
<ref:MessageDetails>
<gen:TestID>
<gen:IdValue>2412665651</gen:IdValue>
<gen:IdScheme>Test</gen:IdScheme>
<gen:IdType>Person</gen:IdType>
</gen:TestID>
</ref:MessageDetails>
<gen:Name>
<gen:StructuredName>
<gen:GivenName>Test</gen:GivenName>
<gen:FamilyName>Test</gen:FamilyName>
</gen:StructuredName>
<gen:NameType>Current Name</gen:NameType>
</gen:Name>
</ref:MessageDataRef>
I want to read every XML element in to a XML Element object. As store them in a List or Array of XML Elements.
//Create new document
var document = new XmlDocument();
document.Load("C:\\Users\\liam.mccann\\Documents\\test.xml");
After loading in in I haven't had any success in getting every element only seem to be able to get the first.
So simple Load XML Document >> Validate XML >> Get Scheme Version from first Element >> Parse To XML Element List.
After parsing i hope it would be easy to restructure the document and save it again. And the document been the same.
Hopefully someone is able to help!
I want to read every XML element in to a XML Element object. As store
them in a List or Array of XML Elements.
You could do the following:
var array = document.SelectNodes("/descendant-or-self::*")
.OfType<XmlElement>()
.ToArray();
Having said that, if all you're intending is:
After parsing i hope it would be easy to restructure the document and
save it again.
then storing the elements in an array is probably not the optimal approach. You can use the XmlDocument class itself (or better yet - the new LINQ approach: System.Xml.Linq.XDocument).
Here's the MSDN document that hopefully can get you started: http://msdn.microsoft.com/en-us/library/bb387084%28v=vs.110%29.aspx.
I need some help with reading an oddly-formatted XML file. Because of the way the nodes and attributes are structured, I keep running into XMLException errors (at least, that's what the output window is telling me; my breakpoints refuse to fire so that I can check it). Anyway, here's the XML. Anyone experienced anything like this before?
<ApplicationMonitoring>
<MonitoredApps>
<Application>
<function1 listenPort="5000"/>
</Application>
<Application>
<function2 listenPort="6000"/>
</Application>
</MonitoredApps>
<MIBs>
<site1 location="test.mib"/>
</MIBs>
<Community value="public"/>
<proxyAgent listenPort="161" timeOut="2"/>
</ApplicationMonitoring>
Cheers
EDIT: Current version of the parsing code (file path shortened - Im not actually using this one):
XmlDocument xml = new XmlDocument();
xml.LoadXml(#"..\..\..\ApplicationMonitoring.xml");
string port = xml.DocumentElement["proxyAgent"].InnerText;
Your problem in loading the XML is that xml.LoadXml expects you to pass the xml document as a string, not a file reference.
Try instead using:
xml.Load(#"..\..\..\ApplicationMonitoring.xml");
Essentially in your original code you are telling it that your xml document is
..\..\..\ApplicationMonitoring.xml
And I'm sure you can now see why there is a parse exception. :) I've tested this with your xml document and the modified load and it works fine (except for the issue that Only Bolivian Here pointed out with the fact that your inner Text is not going to return anything.
For completeness you probably want:
XmlDocument xml = new XmlDocument();
xml.Load(#"..\..\..\ApplicationMonitoring.xml");
string port = xml.DocumentElement["proxyAgent"].Attributes["listenPort"].Value;
//And to get stuff more specifically in the tree something like this
string function1 = xml.SelectSingleNode("//function1").Attributes["listenPort"].Value;
Note the use of the Value property on the attribute and not the ToString method which won't do what you are expecting.
Exactly how you extract the data from the xml is probably dependant on what you are doing with it. For example you may want to get a list of Application nodes to enumerate over with a foreach by doing this xml.SelectNodes("//Application").
If you are having trouble with extdacting stuff though that is probably the scope of a different question since this was just about how to get the XML document loaded.
xml.DocumentElement["proxyAgent"].InnerText;
The proxyAgent element is self closing. InnerText will return the string inside of an XML element, in this case, there is no inner elements.
You need to access an attribute of the element, not the InnerText.
Try this:
string port = xml.GetElementsByTagName("ProxyAgent")[0].Attributes["listenPort"].ToString();
Or use Linq to XML:
http://msdn.microsoft.com/en-us/library/bb387098.aspx
And... your XML is not malformed...
I am trying to use XDocument.Parse(string s) to parse some XML that is being returned from a REST based API. After the XML is parsed, it creates a new XDocument, but the document doesn't contain the properly parsed XML nodes. The name of the first node is the correct node name, but the value is the the concatenation of all the text from the XML, regardless of which Element is belongs to. Can anybody help me figure out what is going on?
XML
<sci_reply version="1.0">
<send_message>
<device id="00000000-00000000-00000000-00000000">
<error id="303">
<desc>Invalid target. Device not found.</desc>
</error>
</device>
<error>Invalid SCI request. No valid targets found.</error>
</send_message>
</sci_reply>
Debug View of XDocument Object
That's the expected behavior. The Value of a an XML element is concatenation of values of all its children. If you want to actually access the XML, read something about LINQ to XML or classes in the System.Xml.Linq namespace.
Thats just the debugger being nice.
The root is being displayed with all of its children.
I have an XML document that would contain empty nodes that looked like the following:
<metadata territory="USA"></metadata>
After simply opening, then saving using XmlDocument, this line looks like:
<metadata territory="USA">
</metadata>
When I set PreserveWhitespace to true, it converted the entire XML to 1 line, so this won't work.
These XML files need to keep the current formatting as much as possible. I know, technically, it doesn't matter which way they are written, they will be read the same way but I still need to keep the same formatting. I can't figure out a way to keep the nodes with no values to 1 line. Is there a way to do this?
The ONLY method that keeps the document in its original formatting is if the XML file contained 'xml:space="preserve"' in the header, but I am to leave the header as is.
The only thing I want to change is the addition of values. As I said, simply loading and saving a document adds this, so if you want to test, just try...
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\Temp\test.xml");
doc.Save(#"C:\Temp\test_02.xml");
Just did the test and this works using both XDocument and XmlDocument by setting the PreserveWhitespace property.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("test.xml");
xmlDoc.Save("testOut.xml");
..
XDocument xdoc = XDocument.Load("test.xml", LoadOptions.PreserveWhitespace);
xdoc.Save(#"testOut.xml");
Input:
<foo>
<metadata territory="USA"></metadata>
<bar></bar>
<baz>
</baz>
</foo>
Output:
<foo>
<metadata territory="USA"></metadata>
<bar></bar>
<baz>
</baz>
</foo>
I'm with Richard Schneider: I don't believe it's possible. One possible solution is to take the output XML file and run it through an XML formatting program that normalizes the format of the XML file (you can probably write one with the unmanaged XML dom if one can't be found).
Since the file is always normalized, it won't change that much hopefully.
If you're using xmlDocument, I may recommend you to use XDocument instead (Framework 3.0+).
PreserveWhitespace will add a
<whatever> <...>
**</whatever>**
to each line while None will just close it like <... />.
I looked up for 5 minutes how to preserve those white space, but couldn't find it. There's something ommitting char(13) in the de/reserialization.
XDocument doc;
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
//Alternative with .None
doc = XDocument.Load(fs, LoadOptions.PreserveWhitespace);
}
and importantly..
xmlDoc.Save("lala.xml", SaveOptions.None);
I don't think this is possible. When you load the XML document you lose formatting information; so there is no way that Save can give the same results.
Why not save the file as a different format, then rename it back to XML after it's been saved. I would be surprised if it still gets formatted incorrectly. Not pretty but pretty easy.
If I get the path to a specific node as a string can I somehow easily find said node by using Linq/Method of the XElement ( or XDocument ).
There are so many different types of XML objects it would also be nice if as a added bonus you could point me to a guide on why/how to use different types.
EDIT: Ok after being pointed towards XPathSelectElement I'm trying it out so I can give him the right answer I can't quite get it to work though. This is the XML I'm trying out
<Product>
<Name>SomeName</Name>
<Type>SomeType</Type>
<Quantity>Alot</Quantity>
</Product>
and my code
string path = "Product/Name";
string name = xml.XPathSelectElement(path).Value;
note my string is coming from elsewhere so I guess it doesn't have to be literal ( at least in debug mode it looks like the one above). I've also tried adding / in front. It gives me a null ref.
Try using the XPathSelectElement extension method of XElement. You can pass the method an XPath expression to evaluate. For example:
XElement myElement = rootElement.XPathSelectElement("//Book[#ISBN='22542']");
Edit:
In reply to your edit, check your XPath expression. If your document only contains that small snippet then /Product/Name will work as the leading slash performs a search from the root of the document:
XElement element = document.XPathSelectElement("/Product/Name");
If there are other products and <Product> is not the root node you'll need to modify the XPath you're using.
You can also use XPathEvaluate
XDocument document = XDocument.Load("temp.xml");
var found = document.XPathEvaluate("/documents/items/item") as IEnumerable<object>;
foreach (var obj in found)
{
Console.Out.WriteLine(obj);
}
Given the following xml:
<?xml version="1.0" encoding="utf-8" ?>
<documents>
<items>
<item name="Jamie"></item>
<item name="John"></item>
</items>
</documents>
This should print the contents from the items node.