C# Deserilize from xml to object (<result xmlns=''> was not expected.) - c#

I try to desalinize from XML to object. When i try to execute this code i get this inner exception get error message was not expected.. Please help me. Thank you for all reply.
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>
<interfaceId>shop.shipping.segment.get</interfaceId>
<systemStatus>OK</systemStatus>
<message>OK</message>
<requestId>714a4983-555f-42d9-aeea-89dae89f2f55</requestId>
<requests>
<id>1</id>
<kbnId>1</kbnId>
</requests>
</status>
<tns:shopMngApiResponse xmlns:tns="http://rakuten.co.jp/rms/mall/shop/mng/api/model/resource">
<resultCode>N000</resultCode>
<resultMessageList>
<resultMessage>
<code>N000</code>
<message>Succeeded.</message>
</resultMessage>
</resultMessageList>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tns:soryoKbnResourceModel">
<soryoKbnList>
<soryoKbn>
<shopId>202317</shopId>
<kbnId>1</kbnId>
<id>1</id>
<name>test name 1</name>
</soryoKbn>
<soryoKbn>
<shopId>202317</shopId>
<kbnId>2</kbnId>
<id>7</id>
<name>test name 2</name>
</soryoKbn>
</soryoKbnList>
</result>
</tns:shopMngApiResponse>
</result>
Source Code: https://app.box.com/s/mmuk2ndkmz4llb71ryw81rpzca9mrmge

When deserializing into the "result"- object set the property as following:
[XmlElement(ElementName = "result", Form = XmlSchemaForm.Qualified)]
public YOURRESULTOBJECT result { get; set; }

Related

C# Select SingleNode Issue

The following Statement not Working XML below ? I am getting a Null Returned. See XML below.
var nodesql = xmlDoc.SelectSingleNode("//CUSTOMERS/CUSTOMER[CODE='AHLENS']");
<?xml version="1.0" encoding="utf-8"?>
<CUSTOMERS>
<CUSTOMER CODE="LABELS" SELECTSQL="select
SQUEEZE(PACK_REF),':',
SQUEEZE(CARTON_NO),':',
SQUEEZE(TOT_CARTONS),':',
SQUEEZE(CUST_SHNAME),':',
SQUEEZE(CUST_SHADDRESS_1),':',
SQUEEZE(CUST_SHADDRESS_2),':',
SQUEEZE(CUST_SHADDRESS_3),':',
SQUEEZE(CUST_SHADDRESS_4),':',
SQUEEZE(CUST_SHADDRESS_5),':',
SQUEEZE(REF_DET),':',
SQUEEZE(CARTON_SSCC_NUMBER)
from ct_generic" LOCATION="D:\Rendez\source_code\C#\CartonLabelPrinting\CartonLabelPrinting\CartonClasses\\LABELS\LABELS.SLD" />
<CUSTOMER CODE="ACKERMANN" SELECTSQL="select
SQUEEZE(PACK_REF),':',
SQUEEZE(CARTON_NO),':',
SQUEEZE(TOT_CARTONS),':',
SQUEEZE(CUST_SHNAME),':',
SQUEEZE(CUST_SHADDRESS_1),':',
SQUEEZE(CUST_SHADDRESS_2),':',
SQUEEZE(CUST_SHADDRESS_3),':',
SQUEEZE(CUST_SHADDRESS_4),':',
SQUEEZE(CUST_STYLE_NO),':',
SQUEEZE(CUST_SIZE),':',
SQUEEZE(CARTON_PCS),':',
SQUEEZE(REF_DET),':',
SQUEEZE(BARCODE)
from ct_generic" LOCATION="D:\Rendez\source_code\C#\CartonLabelPrinting\CartonLabelPrinting\CartonClasses\\ACKERMANN\ACKERMANN.SLD" />
<CUSTOMER CODE="AHLENS" SELECTSQL="select
SQUEEZE(PACK_REF),':',
SQUEEZE(CARTON_NO),':',
SQUEEZE(TOT_CARTONS),':',
SQUEEZE(CUST_SHNAME),':',
SQUEEZE(CUST_SHADDRESS_1),':',
SQUEEZE(CUST_SHADDRESS_2),':',
SQUEEZE(CUST_SHADDRESS_3),':',
SQUEEZE(CUST_SHADDRESS_4),':',
SQUEEZE(CARTON_SSCC_NUMBER),':',
SQUEEZE(CUST_FWD_ADDRESS_1),':',
SQUEEZE(CUST_FWD_ADDRESS_2),':',
SQUEEZE(CUST_FWD_ADDRESS_3),':',
SQUEEZE(CUST_FWD_ADDRESS_4),':',
SQUEEZE(CARTON_WT),':',
SQUEEZE(CARTON_PCS),':',
SQUEEZE(REF_DET),':',
SQUEEZE(CUST_CODE),':',
SQUEEZE(CUST_FWD_ADDRESS_5)
from ct_generic" LOCATION="D:\Rendez\source_code\C#\CartonLabelPrinting\CartonLabelPrinting\CartonClasses\\AHLENS\AHLENS.SLD" />
<CUSTOMER CODE="AMAZON" SELECTSQL="select
SQUEEZE(PACK_REF),',',
SQUEEZE(CUST_CODE),',',
SQUEEZE(CARTON_SSCC_NUMBER),',',
SQUEEZE(CUST_SHNAME),',',
SQUEEZE(CUST_SHADDRESS_1),',',
SQUEEZE(CUST_SHADDRESS_2),',',
SQUEEZE(CUST_SHADDRESS_3),',',
SQUEEZE(CUST_SHADDRESS_4),',',
SQUEEZE(CUST_SHADDRESS_5),',',
SQUEEZE(REF_DET),',',
SQUEEZE(CARTON_PCS),',',
SQUEEZE(CARTON_NO),',',
SQUEEZE(TOT_CARTONS)
from ct_generic" LOCATION="D:\Rendez\source_code\C#\CartonLabelPrinting\CartonLabelPrinting\CartonClasses\\AMAZON\AMAZON.SLD" />
</CUSTOMERS>
You need to Prefix with # in front of Code to read the node based on Attribute
var nodesql = xmlDoc.SelectNodes("/CUSTOMERS/CUSTOMER[#CODE='AHLENS']");
var xmlAttribute = nodeSql.Attributes;
string sql3 = xmlAttribute["SELECTSQL"].Value;

how to update a xml using a id in C#2.0

I am trying to update a xml on basis of id.
my xml file looks like this
<?xml version="1.0" standalone="yes"?>
<CATALOG>
<CD>
<ID>0</ID>
<HeaderDetailID>0</HeaderDetailID>
<FirstName>
</FirstName>
<LastName>
</LastName>
<EmployeeID>
</EmployeeID>
<Department>
</Department>
<Postion>
</Postion>
<Application>
</Application>
<Filter>
</Filter>
<AreaorCountryorStation>
</AreaorCountryorStation>
<NetworkDomain>
</NetworkDomain>
<Action>
</Action>
<NameOfController>
</NameOfController>
<Status>
</Status>
</CD>
<CD>
<ID>1</ID>
<HeaderDetailID>1</HeaderDetailID>
<FirstName>Basant</FirstName>
<LastName>Basant</LastName>
<EmployeeID>Basant</EmployeeID>
<Department>Basant</Department>
<Postion>Basant</Postion>
<Application>
</Application>
<Filter>
</Filter>
<AreaorCountryorStation>Basant</AreaorCountryorStation>
<NetworkDomain>Basant</NetworkDomain>
<Action>
</Action>
<NameOfController>Basant</NameOfController>
<Status>Request Completed</Status>
</CD>
</CATALOG>
Now I try to fetch id from hidden field and get It on server side to update the node name status.
for (int i = 0; i < DateData.Length - 1; i++)
{
string iRecordID = DateData[i];
XmlDocument xmlDoc = new XmlDocument();
string filepathsUpdate = Server.MapPath("Contact.xml");
xmlDoc.Load(filepathsUpdate);
XmlNode node = xmlDoc.SelectSingleNode("/CATALOG/CD[ID=" + DateData[i] + "]/Status");
node.InnerText = "Request Completed";
xmlDoc.Save(filepathsUpdate);
}
It works locally fine but on server it doesn't work.
Can I close the connection in XML file
I feel like xmlDoc.Save not working

xmldocument add namespace prefix in same node

I want the output to be like this:
<Order xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gml="example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd"
xsi:noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">
The code I am using to get this is :
xmlDocument.DocumentElement.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
xmlDocument.DocumentElement.SetAttribute("xmlns:gml", "example.com");
xmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlDocument.DocumentElement.SetAttribute("xsi:schemaLocation", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");
xmlDocument.DocumentElement.SetAttribute("xsi:noNamespaceSchemaLocation","http://www. xyz.co.uk/xmlorders3/lig_xml_orders.xsd");
But I am getting the output like this :
<Order xmlns:gml="example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd"
noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">
Please suggest me any other way to get the required output.
You should use another version of SetAttribute method:
xmlDocument.DocumentElement
.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");
i found way out. it may not be the best way. the solution is :
string xmlString = xmlDocument.InnerXml.ToString(); xmlString= xmlString.Replace("schemaLocation", "xsi:schemaLocation"); xmlString= xmlString.Replace("noNamespaceSchemaLocation", "xsi:noNamespaceSchemaLocation"); xmlDocument.LoadXml(xmlString);
// now i have the xmlDocument as i required.

Getting the details of an ONVIF FaultException

This answer explains how to get the text of a SOAP FaultException, but it only works when the contents are a serialised string, resulting in <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">details</string>.
This ONVIF device howevers uses the SOAP <env:Text> element, which can't be deserialised to a string.
How can I read the detaisl of the following FaultException?
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:enc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rpc="http://www.w3.org/2003/05/soap-rpc" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:ter="http://www.onvif.org/ver10/error" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:Action>http://www.w3.org/2005/08/addressing/soap/fault</wsa:Action>
</env:Header>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Receiver</env:Value>
<env:Subcode>
<env:Value>ter:Action</env:Value>
</env:Subcode>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">ActionFailed</env:Text>
</env:Reason>
<env:Detail>
<env:Text>It is not possible to operate because of the unsupported video source mode.</env:Text>
</env:Detail>
</env:Fault>
</env:Body>
</env:Envelope>
This code is from the linked answer above:
} catch (FaultException ex) {
System.ServiceModel.Channels.MessageFault mf = ex.CreateMessageFault();
if (mf.HasDetail) {
string detailedMessage = mf.GetDetail<string>();
output.OutputText(detailedMessage);
}
}
Which fails with:
An exception of type
'System.Runtime.Serialization.SerializationException' occurred in
System.Runtime.Serialization.dll but was not handled in user code
Additional information: Expecting element 'string' from namespace
'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered
'Element' with name 'Text', namespace
'http://www.w3.org/2003/05/soap-envelope'.
There must be a built in deserialiser for it as the the <env:Reason> element uses the same node.
You can get the detail as a "raw" XmlElement:
System.ServiceModel.Channels.MessageFault mf = ex.CreateMessageFault();
if (mf.HasDetail) {
System.Xml.XmlElement detailedMessage = mf.GetDetail<System.Xml.XmlElement>();
System.Diagnostics.Trace.WriteLine("Detail: " + detailedMessage.InnerText);
}
This should only be used when you know what the contents will be, unless wrapped in a try { } catch { } block

Not able to get the element with XDocument

Below is the code i am using to get the value of the 'name'(attribute) of the 'person'
parameter tag="person" and parameter attribute="name"
public static string GetInformationFromXML(string tag,
string attribute,
string filePath)
{
XDocument doc = XDocument.Load(filePath);
string info = doc.Element(tag).Attribute(attribute).Value;
return info;
}
doc.Element(tag) is not getting the element even though when i expand doc, it does have an element with type 'Element' and Name 'person'
the file being read is XmlDocument for your information.
below is the xml of the file i am trying to read
<?xml version="1.0"?>
<import>
<company name1="ABC" name2="" action="create"
profile="\Profiles\ABC\" id="C1">
<address street="industrial" city="london"
country="england" id="A1">
<telecom type="phone" value="4839282992"
desc="" default="true" />
<telecom type="fax" value="3232" desc="" />
</address>
</company>
<person title="Mr." name="Tariq" surname="sheikh"
lang="EN" action="create" profile="Profiles\Tariq"
login="tariq" password="123456" default_address="A1">
<link reference="C1" type="Employee" description="Software developer" />
<address street="baker street" zip="12443"
city="london" country="england" />
<account bank="Barclays" account="4378734834" />
<telecom type="email" value="tariq.sheikh#abc.co.in" desc="" />
<registration type="temporaryID"
value="4623648c-739e-49c8-93fa-41dc7fed53ea" />
</person>
</import>
I am new to XDocument !
You have to use Descendants when element you're looking for is not direct child of current element (or root for XDocument).
string info = doc.Descendants(tag).First().Attribute(attribute).Value;

Categories