I am trying to read this XML document with XDocument in c sharp.
<Instrument_Root>
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
I use the below code to read the XML doc:
XDocument xDoc = XDocument.Load("XMLFile1.xml");
I receive an error message because of the xsi:nil
Error merssage:An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll 'xsi' not declared.
Is there a way to treat xsi as NULL?
Thank you
The namespace prefix xsi has to be declared somewhere for the document to be qualified as XML, for example :
<Instrument_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
You can try to change the XML files to look like this:
<Instrument_Root xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Instrument_ID>123</Instrument_ID>
<Deal_number xsi:nil="true"/>
</Instrument_Root>
screencapture
Related
I have am xml string like mentioned below:
<?xml version="1.0" encoding="utf-8" ?>
<NodeA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.air-watch.com/webapi/resources">
<AdditionalInfo>
<Links>
<Link xsi:type="link">
</Link>
</Links>
</AdditionalInfo>
<TotalResults>100</TotalResults>
<NodeB>
<NodeC>
<Id>1</Id>
<A>valueA</A>
<B>valueB</B>
</NodeC>
<NodeC>
<Id>2</Id>
<A>valueA</A>
<B>valueA</B>
</NodeC>
</NodeB>
</NodeA>
I want to extract NodeB and its child nodes (NodeC elements). How can I do it? Below solution does somewhat similar operation but it needs the xml string to be loaded in a XDocument first:
XDocument doc=XDocument.Parse(xmlstr);
String response=doc.Elements("question")
.Where(x=>x.Attribute("id")==id)
.Single()
.Element("response")
.Value;
Is there a way to do it without having to load it in a doc? Some operation on string object itself.
Why cant you use this
XDocument doc=XDocument.Parse(xmlstr);
String response=doc.Elements("question")
.Where(x=>x.Attribute("id")==id)
.Single()
.Element("response")
.Value; ?
you can use Regular Expressions then.
I am trying to understand how I can validate an XML file with an XSD.
I don't quite understand the namespaces.
If I have the code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(file.InputStream);
xDoc.Schemas.Add("http://www.w3.org/2001/XMLSchema-instance", #"C:MyXSD.xsd");
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
xDoc.Validate(eventHandler);
I get the Error:
"The target namespace of an attribute declaration, whether local or global, must not match http://www.w3.org/2001/XMLSchema-instance."
What should I be setting the namespace to when I add the schema to the xDoc?
I have an xml like this:
<?xml version="1.0"?>
<rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// My Nodes
</rootNode>
and I created an XSD from that XML.
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:element name="rootNode">
// My Nodes
</xsd:element>
</xs:schema>
In your scenario you can just pass null as the namespace to Schemas.Add, as you are not actually using namespaces, i.e. your XML document does not use a namespace (which would be specified either with the xmlns attribute or the use of a namespace prefix).
For more information on namespaces please see http://www.w3schools.com/xml/xml_namespaces.asp.
Note that http://www.w3.org/2001/XMLSchema-instance is a namespace used to define schema attributes that relates only to the schema infrastructure and not your specific XML schema.
I am getting the following exception when I am trying to deserialize the xml document. Xml document has a tag as url in which google search link may present. Google search link contains '=' which is not accepted in the xml document while deserializing it. I am getting the xml from server. So I cannot do anything with the string that is present in the url tag. I have to do something on my client part. How can I overcome this problem?
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<code>000</code>
<message>Successfully completed</message>
</status>
<reports>
<report>
<id>9973</id>
<url>http://www.google.com/search?q=guns&client=safari&safe=active</url>
</report>
</reports>
</response>
Exception :
An exception of type 'System.InvalidOperationException' occurred in System.Xml.XmlSerializer.dll but was not handled in user code
Innerexception:
{"'=' is an unexpected token. The expected token is ';'. Line 136, position 53."}
Your XML is invalid. The URL is breaking the XML standard. Specifically you should escape the &: &.
This is the valid XML:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<code>000</code>
<message>Successfully completed</message>
</status>
<reports>
<report>
<id>9973</id>
<url>http://www.google.com/search?q=guns&client=safari&safe=active</url>
</report>
</reports>
</response>
Check your XML export function to make sure it escapes the URL properly.
I added a reference to a Web Service in my project. I'm using the generated code to call the methods of the Web Service. One of the methods returns the following structure:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:typens="urn:AllegroWebApi" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<typens:doGetCatsDataResponse>
<cats-list xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="typens:CatInfoType[24051]">
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">26013</cat-id>
<cat-name xsi:type="xsd:string">Antyki i Sztuka</cat-name>
<cat-parent xsi:type="xsd:int">0</cat-parent>
<cat-position xsi:type="xsd:int">0</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item>
....
</item>
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">124895</cat-id>
<cat-name xsi:type="xsd:string">PozostaĆe</cat-name>
<cat-parent xsi:type="xsd:int">124883</cat-parent>
<cat-position xsi:type="xsd:int">5</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item>
<item xsi:type="typens:CatInfoType">
<cat-id xsi:type="xsd:int">124894</cat-id>
<cat-name xsi:type="xsd:string">Teleskopy</cat-name>
<cat-parent xsi:type="xsd:int">124883</cat-parent>
<cat-position xsi:type="xsd:int">6</cat-position>
<cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
</item> // Line 168361 <--------------- HERE
</cats-list>
<ver-key xsi:type="xsd:long">91632766</ver-key>
<ver-str xsi:type="xsd:string">1.1.47</ver-str>
</typens:doGetCatsDataResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The generated code that is responsible for deserializing it throws the following exception:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Xml.dll Additional information: There is an error
in XML document (168361, 13).
I marked the line that is mentioned in the exception in the code sample. I guess it has something to do with the fact that it is the closing tag of the last item object. I have no idea why it is failing. The XML is well formated and does not seem to include any unsupported characters.
Just a guess, but check your data, could be one of the fields has a '<' or '>' throwing off the formatting of the xml document.
I see you have two closing item tags in a row. Granted, you may be excluding some of the XML file. Still, two closing tags in a row would certainly cause that error.
Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)