Need help writing and reading XML file in C# - c#

Idea - List of vertices(Key, X, Y, Priority to store).
<?xml version="1.0" encoding="utf-8"?>
<Vertices>
<Vertex Key="0" X="149" Y="209" Priority="7" />
<Vertex Key="1" X="278" Y="128" Priority="7" />
</Vertex>
Is this valid XML? It keeps saying me that root element is missing, when i try to open it... If so, can someone provide a valid c# XDocument code to open this file ?

It's not valid XML - your closing element has the wrong name - this would be valid:
<?xml version="1.0" encoding="utf-8"?>
<Vertices>
<Vertex Key="0" X="149" Y="209" Priority="7" />
<Vertex Key="1" X="278" Y="128" Priority="7" />
</Vertices>
Also make sure that if you are loading an XML file you use XDocument.Load and not XDocument.Parse.

You are opening <Vertices> but closing </Vertex>. Need to change that last closing tag to </Vertices>
Side note:
If you load an XML file into Visual Studio it will tell you if it is invalid XML and why. For this example it gave the errors:
Error 1 Tag was not closed. XMLFile1.xml Line 2 Column 5
Error 2 Expecting end tag </Vertices>. XMLFile1.xml Line 5 Column 6
If you do not own Visual Studio, you can download the Express version for free and get the same functionality.

Related

Unexpected XML Declaration. XML Declaration must be the first node in the document and no whitespace characters are allowed to appear before it linit [duplicate]

This error,
The processing instruction target matching "[xX][mM][lL]" is not allowed
occurs whenever I run an XSLT page that begins as follows:
<?xml version="1.0" encoding="windows-1256"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="../header.xsl"/>
<xsl:template match="/">
<xsl:call-template name="pstyle"/>
<xsl:call-template name="Validation"/>
<xsl:variable name="strLang">
<xsl:value-of select="//lang"/>
</xsl:variable>
<!-- ////////////// Page Title ///////////// -->
<title>
<xsl:value-of select="//ListStudentFinishedExam.Title"/>
</title>
Note: I removed any leading spaces before the first line, but the error still occurs!
Xerces-based tools will emit the following error
The processing instruction target matching "[xX][mM][lL]" is not allowed.
when an XML declaration is encountered anywhere other than at the top of an XML file.
This is a valid diagnostic message; other XML parsers should issue a similar error message in this situation.
To correct the problem, check the following possibilities:
Some blank space or other visible content exists before the <?xml ?>
declaration.
Resolution: remove blank space or any other
visible content before the XML declaration.
Some invisible content exists before the <?xml ?>
declaration. Most commonly this is a Byte Order Mark
(BOM).
Resolution:
Remove the BOM using techniques such as those suggested by the W3C
page on the BOM in HTML.
A stray <?xml ?> declaration exists within the XML content.
This can happen when XML files are combined programmatically or
via cut-and-paste. There can only be one <?xml ?> declaration
in an XML file, and it can only be at the top.
Resolution: Search for
<?xml in a case-insensitive manner, and remove all but the top XML
declaration from the file.
Debug your XML file. Either there is space or added extra or fewer tags.
For better understanding build the project through the command line. Windows: gradlew build
In my case, AndroidManifest.xml has a blank space at the very first line
<Empty Row> // This Creates the issue
<?xml version="1.0" encoding="utf-8"?>
There was auto generated Copyright message in XML and a blank line before <resources> tag, once I removed it my build was successful.
just remove this line: <?xml version="1.0" encoding="utf-8"?> because this kind of error only come because of this line or you might also check the format of your line according the mentioned line in this answer.
I had a similar issue with 50,000 rdf/xml files in 5,000 directories (the Project Gutenberg catalog file). I solved it with riot (in the jena distribution)
the directory is cache/epub/NN/nn.rdf (where NN is a number)
in the directory above the directory where all the files are, i.e. in cache
riot epub/*/*.rdf --output=turtle > allTurtle.ttl
This produces possibly many warnings but the result is in a format which can be loaded into jena (using the fuseki web interface).
surprisingly simple (at least in this case).
Another reason of the above error is corrupted jar file. I got the same error but for Junit when running unit tests. Removing jar and downloading it again fixed the issue.
in my case was a wrong path in a config file: file was not found (path was wrong) and it came out with this exception:
Error configuring from input stream. Initial cause was The processing
instruction target matching "[xX][mM][lL]" is not allowed.
For PHP, put this line of code before you start printing your XML:
while(ob_get_level()) ob_end_clean();
It's worth checking your server's folders to see if there's a stray pom.xml hanging around.
I found that I had the problem everyone else described with a malformed pom.xml, but in a folder that I didn't expect to be on the server. An old build was sticking around unwelcome D:
For my case, the tab is the trouble maker. Replace the tab with blank should resolve the issue

how to get xml content and edit xml file by using C#

I have an XML file called Emails.xml:
<Root>
<Emails>
<address>dfg#fds.com</address>
</Emails>
<Emails>
<address>adsfZSdf#.com</address>
</Emails>
</Root>
Um...I'm using visual studio, asp.net. I want to get the addresses by using C# code and also edit one or more addresses(e.g. chage "dfg#fds.com" to "ddfla#fds.com").
Furthermore, add new address(es) to this xml file.
(path of xml file is: C:\Documents and Settings\Administrator\Desktop\Emails.xml)
How can I do that?
Use the XmlDocument class. Then you can filter to the addresses with a call to GetElementsByTagName(string). From there, modification should be easy.

"Cannot Serialize Multidimensional Arrays" -What is it, and what did I type wrong?

I've been trying to Import XML files into my XNA project for quite a while.
Just when I thought that I'd gotten it all done, this error pops up. Lots of searching, and I haven't found an answer that helps.
This is a clipped version of my XML document:
<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<!-- TODO: replace this Asset with your own XML asset data. -->
<Asset Type="Library.Room">
<Tile itIs="Wall">
<x>0</x>
<y>0</y>
<o>0</o>
</Tile>
</Asset>
</XnaContent>
When trying to Build, I get this Error:
"Building content threw RankException: Cannot serialize multidimensional arrays."
Even commenting out lines of my project code do nothing to change this error, so I know that it's something in how I've written my XML. ( Which I thought was okay )
So, that tells me that I'm probably writing my XML wrong. What is a MultiDimensional Array in XML, and how can I avoid it?
___Added Info
I add the XML info to an Array here, piece by piece. --naturally, I thought that this was the problem at first, but commenting any of this out has no effect.
public void loadFromFile(string fileName, Room r)
{
var doc = XElement.Load(fileName);
foreach (var v in roomDoc.Elements("Tile"))
{
r.blueprint.SetValue(new Things.Wall((int)v.Element("o")), (int)v.Element("x"), (int)v.Element("y"));
}
}

Programmatic XML Diff / Merge in C#

At this moment, I am managing a piece of software that has multiple XML configuration files. When a new version of software is released, sometimes the base config files change, we currently have the software call KDiff on startup. If it detects a change, it prompts the user to choose the changes.
The problem with this approach is that KDiff is a line comparing program and not aware of the ways of XML (like Nodes, etc.)
Ideally, I would like to programmatically work with a library in C# (since we're a MS shop) that can Diff two XML files: a Source XML and a Current Working XML.
And then Merge the two together using a few simple rules:
If the Current Working XML has a node that the Source XML does not, remove it.
If the Source XML has a node that the Current Working XML does not, add it.
If both have the same node and the values differ, favor the Source XML's value, unless it the Source XML's value is set to "UseExistingValue".
For example, here's the "Source" XML:
<Configuration>
<Items>
<Item Id="1" Position="true">
<Location X="UseExistingValue" Y="UseExistingValue" Z="UseExistingValue" />
<Something/>
<SomethingElse/>
</Item>
</Items>
</Configuration>
And here's the "Current Working" XML:
<Configuration>
<Items>
<Item Id="1" Position="false">
<Location X="123" Y="234" Z="345" />
<Another/>
<Something/>
</Item>
</Items>
</Configuration>
And the merged version would look like:
<Configuration>
<Items>
<Item Id="1" Position="true">
<Location X="123" Y="234" Z="345" />
<Something/>
<SomethingElse/>
</Item>
</Items>
</Configuration>
I've looked at the MS XML Diff and Patch Tool and it definitely merges the files together, but doesn't allow for the programmatic rules that I want to define.
XMLUnit for Java devs seems promising, but the .NET version of it seems underdeveloped, which is unfortunate.
Anyone have any suggestions for either scriptable XML Diff/Merge tools and/or .NET libraries (paid or free)?
Thanks.
After a couple days of messing around, I found a solution that I think works for me. Maybe it could work for other people as well.
The MS XML Diff and Patch tool was a viable option. When you Diff first file against the second file it creates an XML "DiffGram" listing what changes it detected between the two XML files.
To take care of all 3 rules that I listed above, I Diff'd the two files in one direction, then opened the DiffGram file using Linq-to-XML and Removed all the "Add" and "Remove" lines.
XNamespace xd = "http://schemas.microsoft.com/xmltools/2002/xmldiff";
var doc = XDocument.Load(_diffGramFile);
doc.Root.DescendantsAndSelf(xd + "add").Remove();
doc.Root.DescendantsAndSelf(xd + "remove").Remove();
Then I patched up (merged) this edited diffgram against the first file and created a partially merged temporary file. This takes care of Rules 1 and 2.
Next, I Diff'd the partially merged file against the first file used. Then opened the new DiffGram and removed all Change references to "UseExistingValue".
var newdoc = XDocument.Load(_diffGramFile);
newdoc.Root.DescendantsAndSelf(xd + "change")
.Where(x => x.Value == "UseExistingValue").Remove();
And merged this edited DiffGram against the partially merged file which takes care of Rule 3. Saving this out to XML then produces the final XML merged according to the rules defined above.
Hopefully this can help out other people.
HINT: After installing the XmlDiffPatch library, the XmlDiffPatch DLL can be found in C:\Windows\assembly\GAC\XmlDiffPatch\1.0.8.28__b03f5f7f11d50a3a\XmlDiffPatch.dll

XML tag meaning

I have a part of xml file
<Text><?xml version="1.0" encoding="utf-16"?>
<ObjectFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FilterConditions>
<FilterCondition>
<PropertyFilters>
<PropertyFilter>
<PropertyName>Message</PropertyName>
<FilterValue xsi:type="xsd:string">PPM exceeds tolerance</FilterValue>
<FilterType>strExpr</FilterType>
<Operator>eq</Operator>
<CaseSensitive>true</CaseSensitive>
<Recursive>false</Recursive>
</PropertyFilter>
</PropertyFilters>
<Enabled>true</Enabled>
<ObjectTypeName>Spo.DataModel.UnixLogMessage</ObjectTypeName>
<ObjectClassGR>
<Guid>00000000-0000-0000-0000-000000000000</Guid>
</ObjectClassGR>
Here what is node Recursive meant,,it actually like this <Recursive>false</Recursive>
but how come it like &lt ;Recursive>false&lt ;/Recursive >
Can any one help me about this
How are you getting this XML file? From a webpage?
It seems that the way you are getting the text file is translating it as an HTML document and thus turning your '<' into &lt and your '>' into &gt
You need to ensure that the page is not interpreted as HTML. You could just copy-paste everything into Notepad first for a simple solution.

Categories