Adding values to an InfoPath XML schema - c#

I have an InfoPath form in a SharePoint workflow. I'm trying to use a blank copy of the XML produced by the InfoPath to create new instances of the form for the document library to start the workflow, thats not where my problem is. I have an app which copies the file to the document library but when i try to populate the XML i get this error:
Data at the root level is invalid.
Line 1, position 1
at the line which reads
doc.LoadXml("copiedFile.xml");
I have no idea why it does the, as to my knowledge the XML is well formed (as this is done automatically by InfoPath) so i can't see where the problem is.
the first four lines of the XML are as follows:
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:InfoPathForm:-myXSD-2009-10-12T13-20-27" solutionVersion="1.1.0.84" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://seed-dev1/FormServerTemplates/InfoPathForm%5B3%5D.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-10-12T13:20:27" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">

Could try giving...
doc.Load("copiedFile.xml");
a go.

Your XML document header seems ok, but I can bet on encoding-related problems.
Can you please post your code to create that XML file? Have you noted a "strange" first character in your file content?

I have had this sort of problem before. I am not sure what caused it, propbably the encoding.
Open the file in a program like notepad2. Whatever you use, You need to see the whitespace. The first couple of charaters will be gibberish. delete the whitespace and then save the document.
then give your app ago.
Hopefully it will work for you.

Related

How to parse an xml that has non-xml data in it

I am working with some xml in C# and am having some issues parsing an xml file due to the format it is in. It has non xml data in the file and I have no control over the format of this file. The file is "test.xml"(see below). I am only concerned with the xml portion of the data, but am unsure the best way to go about accessing it. Any thoughts or recommendations would be greatly appreciated.
Test data -1
Smith, 2234
##*j
Random--
#<?xml version="1.0" encoding="utf-16"?>
<ConfigMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.Test.com/schemas/Test.test.Config">
<Config>
<Version>10</Version>
<Build>00520</Build>
<EnableV>false</EnableV>
<BuildL>22</BuildL>
<BuildP>\\testpath\test</BuildP>
</Config>
</ConfigMessage>
#
Put the whole file into a string that contains anything within the first '<' and the last '>' characters detected on the file. Then you can treat it as normal XML from there. If there's random non-XML elements throughout it though you will need to add additional logic to detect starting/stopping XML "blocks".
I can suggest you such solution: open your pseudo-xml like simple text-file, read whole text, after that, with using regex you ought to take xml document (part of primordial document that is able to be converted to XML [|startTag|any symbols|/endTag|]), put it into XDocument (in memory) and now parse it like XML-file.

C# multiple writeattributestring to xml not showing in correct order

xtw.WriteStartElement("cXML");
xtw.WriteAttributeString("payloadID", payloadidstr);
xtw.WriteAttributeString("timestamp", utctime());
xtw.WriteAttributeString("version", "1.2.024");
above code working fine to generate xml attribute. if open xml file in notepad shows the following string which is correct.
cXML payloadID="1392408819113-4172669982087053277#123.456.789.10" timestamp="2014-02-14T12:13:39-08:00" version="1.2.024"
but when open xml file in any browser the attribute order is changed showing like this.
cXML version="1.2.024" timestamp="2015-01-15T16:54:48-08:00" payloadID="150120150454480293-832257153#123.456.789.10"
Can someone let me know why browser not showing in correct order or how to shows multiple string under one element.
XML does not define ordering of attributes, so there is no "correct" order - compliant reader/writers are free to order the way they pleased.
Per the spec section 3.1:
the order of attribute specifications in a start-tag or empty-element tag is not significant

Remove Empty Line From XML Document

I am Currently Facing A problem. I am loading a xml file in C# and remove some nodes from it and appending some nodes. now problem is that when i am doing removal from the xml file then there are some empty lines created automatically ,so i want to remove these line .
And when i append some nodes to the parent node in xml then i want the new line in each ending tag
For Eg. My Xml file is
<intro id="S0001">
<title>Introduction Title</title>
<para>This is a paragraph. Note that paragraphs can contain other block–level objects, such as lists, as well as directly containing text.</para>
<para>The introduction can contain all of the text objects that a section can contain, except that it cannot be divided into parts, sections and sub–sections.</para>
<para>The introduction can contain tables:</para>
</intro><part>
<no>Part A</no> Article Structure <sup>&lpar;Part Title&rpar;</sup><section1 id="S0002">`enter code here`
<no>Sect 1</no>
<title>First Section in Part 1 <sup>&lpar;Section 1 Title&rpar;</sup></title>
<shortsectionhead>Short Section Header</shortsectionhead>
<para>This is a section in the first part of the article.</para>
</section1><section1 id="S0003">
Code:
XmlNode partNnode = xmlDoc.SelectSingleNode("//part");
XmlNode introNode=xmlDoc.SelectSingleNode("//intro");
XmlDocumentFragment newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=partNnode.OuterXml;
introNode.ParentNode.InsertAfter(newNode,introNode);
partNnode.ParentNode.RemoveChild(partNnode);
partNnode = xmlDoc.SelectSingleNode("//part");
nodeList = xmlDoc.SelectNodes("//section1");
foreach (XmlNode refrangeNode in nodeList)
{
newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=refrangeNode.??OuterXml;
partNnode.AppendChild(newNode);
}
Please help me
Thanks in advance
If you load and save a XMl file with C#, then the XML should be formatted correctly (an easy way to format strange looking XML files is just to load and save them with some C# code).
If I understand your question correctly, then you are just not happy with the format of the XML file?
Like you want (A):
</intro><part>
But you get (B):
</intro>
<part>
If that is the question, then, in my eyes, you just want a strange thing. Because...
a) Code doesn't care how the XML file is formatted and
b) The format in (B) is the correct one
If you, for what reason ever, want to change it, then you have to parse through the XML file, opening it as a string and checking manually for closed and opened tags.

XSLT: transfer xml with the closed tags

I'm using XSLT transfer an XML to a different format XML. If there is empty data with the element, it will display as a self-closing, eg. <data />, but I want output it with the closing tag like this <data></data>.
If I change the output method from "xml" to "html" then I can get the <data></data>, but I will lose the <?xml version="1.0" encoding="UTF-8"?> on the top of the document. Is this the correct way of doing this?
Many thanks.
Daoming
If you want this because you think that self closing tags are ugly, then get over it.
If you want to pass the output to some non-conformant XML Parser that is under control, then use a better parser, or fix the one you are using.
If it is out of your control, and you must send it to an inadequate XML Parser, then do you really need the prolog? If not, then html output method is fine.
If you do need the XML prolog, then you could use the html output method, and prepend the prolog after transformation, but before sending it to the deficient parser.
Alternatively, you could output it as XML with self-closing tags, and preprocess before sending it to your deficient parser with some kind of custom serialisation, using the DOM. If it can't handle self-closing tags, then I'm sure that isn't the only way in which it fails to parse XML. You might need to do something about namespaces, for example.
You could try adding an empty text node to any empty elements that you are outputting. That might do the trick.
Self-closed and explicitly closed elements are exactly the same thing in any regard whatsoever.
Only if somewhere along your processing chain there is a tool that is not XML aware (code that does XML processing with regex, for example), it might make a difference. At which point you should think about changing that part of the processing, instead of the XML generation/serialization part.

Problem with node.GetElementsByTagName in C#

I have a really simple XML file that I'm trying to read, but I can't seem to get it working. Here is the XML file:
<?xml version="1.0"?> <Results><One>45364634</One><Two>-1</Two><Three>B</Three></Results>
I am trying to get the contents of two like this:
XmlNode node = doc.DocumentElement.SelectSingleNode("/Results/Two");
or
XmlNodeList list = doc.GetElementsByTagName("Two");
Neither is working. When I copy paste the XML as a string into the XmlDocument, then it works. However, when I use the string I pull out of the response (where I'm getting the XML from), it doesn't work.
I'm wondering if it's something weird like a character issue or not looking at the correct root, but I can't figure it out. Any ideas?
Thanks!
Check the Xml file encoding ...
Is it ansi? utf-8 or utf-16?
Check if the xml was loaded from the file at all. Check if there is any error, see if the document was populated.
I think the document is not being populated when loading from the file.
By your use of the word "response" I am assuming you are passing the xml via http? If so, try using HttpServerUtility.HtmlDecode( xml ) see if that works
Bleh.
Turns out I was returning an XML document within an XML document. That's why printing to the screen looked ok but I couldn't pull it out.
Thanks guys.

Categories

Resources