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;
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; }
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;
I'm stuck with some task here. I want to write to xml file.
Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<lists>
<plannedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</plannedList>
<requestedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</requestedList>
</lists>
then i want to use c# and for example dataset or an other method to add a jobID under the loggedIn Skift. so if Skift-a is logged on, and press a button, i want the xml file to look like this:
<?xml version="1.0" encoding="utf-8"?>
<lists>
<plannedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</plannedList>
<requestedList>
<Skift-a></Skift-a>
<jobID>1<jobID/>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</requestedList>
</lists>
And here is what I've tried so far:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(this.Page.Server.MapPath("~/StoredData/JobsByUsers.xml"));
XmlNode Skift = xmldoc.SelectSingleNode("/requestedList/" + User.Identity.Name.ToString());
XmlNode newJobID = xmldoc.CreateNode(XmlNodeType.Element, "jobID", null);
newJobID.InnerText = topID.ToString();
Skift.AppendChild(newJobID);
xmldoc.Save(this.Page.Server.MapPath("~/StoredData/JobsByUsers.xml"));
<?xml version="1.0" encoding="utf-8"?>
<mappings>
<mapping>
<aID iskey="true">ABC</aID>
<bID iskey="true">DEF</bID>
<SubAccount>PS</SubAccount>
<Account>PS</Account>
</mapping>
<mapping>
<aID isKey="true">GHI</aID>
<bID isKey="true">PFP</bID>
<SubAccount>MS</SubAccount>
<!-- I want to add a new node here, how can I do this -->
</mapping>
<mapping>
<aID isKey="true">MNO</aID>
<bID isKey="true">BBG</bID>
<SubAccount>MAX</SubAccount>
</mapping>
</mappings>
I want to add a new node as mentioned in the above XML. I tried a lot but I could not succeed.
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
foreach (XmlNode node in xmlDoc.SelectNodes("/mappings/mapping"))
{
if (boothIDNode.InnerXml == BoothID)
{
chkBoothIDExists = true;
if (clientIDNode.InnerText == ClientID)
{
chkClientIDExists = true;
for (int j = 2; j < nodelist.Count; j++)
{
columnNode = nodelist[j];
if (columnNode.Name == column.ToString())
{
XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null);
elm.InnerText = value.ToString();
node.AppendChild(elm); // Error comes here
}
}
}
}
}
xmlDoc.Save(filename);
The question is solved. The problem occured due to my silly mistake. Basically there are two xml documnets and I'm creating a new node of other xml documnet due to which the error cames.
THanks all,
XmlDocument xDoc = new XmlDocument();
XmlDocument xmlDoc = new XmlDocument();
ERROR: The node to be inserted is from a different document context
Use something like this, not a c# person but this should help. I think insertafter is what you need:
XmlNode currNode = xDoc.SelectNodes("/mappings/mapping");
XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null);
currNode.InsertAfter(elm, currNode.LastChild);
In order to add a node, consider this example:
XDocument a = XDocument.Parse(#"<?xml version=""1.0"" encoding=""utf-8""?>
<mappings>
<mapping>
<aID iskey="true">ABC</aID>
<bID iskey="true">FPP</bID>
<SubAccount>PS</SubAccount>
<Account>PS</Account>
</mapping>
<mapping>
<aID isKey="true">GHI</aID>
<bID isKey="true">PFP</bID>
<SubAccount>MS</SubAccount>
<!-- I want to add a new node here, how can I do this -->
</mapping>
<mapping>
<aID isKey="true">MNO</aID>
<bID isKey="true">BBG</bID>
<SubAccount>MAX</SubAccount>
</mapping>
</mappings>");
a.Descendants("mapping").Skip(1).First().Add(new XElement("aaa", new XAttribute("id", 1)));
--->
<mappings>
<mapping>
<aID iskey="true">ABC</aID>
<bID iskey="true">FPP</bID>
<SubAccount>PS</SubAccount>
<Account>PS</Account>
</mapping>
<mapping>
<aID isKey="true">GHI</aID>
<bID isKey="true">PFP</bID>
<SubAccount>MS</SubAccount>
<!-- I want to add a new node here, how can I do this -->
<aaa id="1" />
</mapping>
<mapping>
<aID isKey="true">MNO</aID>
<bID isKey="true">BBG</bID>
<SubAccount>MAX</SubAccount>
</mapping>
</mappings>
XDocument doc = XDocument.Load(filepath); // filepath is string
doc.Element(firstnodename).SetElementValue(descendantname,newvalue); //names and value are string
doc.Save(filepath); // This line optional. And filepath like : #"C:\Users\user\desktop\a.xml" or "C:\\Users\\user\\desktop\\a.xml". You should write with ".
You can write longer .Element
ForEx:
doc.Element(firstnode).Element(secondnode).Element(thirdnode).SetElementValue(fourthnode,valueoffourth);