I need to get the count of the inbound transactions and the outbound transactions separately. I have tried everything I know, but nothing is working. Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<records>
<inbound>
<transaction>
<customerID>0002</customerID>
<customerName>bob</customerName>
</transaction>
<transaction>
<customerID>0003</customerID>
<customerName>harry</customerName>
</transaction>
.
.
.
<transaction>
<customerID>0250</customerID>
<customerName>joe</customerName>
</transaction>
</inbound>
<outbound>
<transaction>
<customerID>0002</customerID>
<customerName>bob</customerName>
</transaction>
<transaction>
<customerID>0003</customerID>
<customerName>harry</customerName>
</transaction>
.
.
.
<transaction>
<customerID>0175</customerID>
<customerName>frank</customerName>
</transaction>
</outbound>
</records>
This needs to be in C# and LINQ. Thanks for any help.
Here is how to get the number of transactions for inbounds:
var xdoc = XDocument.Parse(xml);
var c = xdoc.Descendants("inbound").Descendants("transaction").Count();
Related
I have this kind of xml file. Root node is Sim and there are multiple "test" subnodes. Also each "test" has an instructor. Finally each instructor has own name,surname and rank values.
<Sim>
<Test ID="1" Description="test1" Date="17/01/2023">
<Instructor Num="1">
<Name>instructor1</Name>
<Surname></Surname>
<Rank></Rank>
</Instructor>
</Test>
<Test ID="2" Description="test2" Date="16/01/2023">
<Instructor Num="22">
<Name>instructor22</Name>
<Surname></Surname>
<Rank></Rank>
</Instructor>
</Test>
</Sim>
With xelement.load, the test ids of this file are read as they are in the file. I want xelement.load to read after test ids are sorted from largest to smallest. How can i do this? instructor part is also included in second code.
<Sim>
<Test ID="3" Description="test3" Date="18/01/2023"></Test>
<Test ID="2" Description="test2" Date="17/01/2023"></Test>
<Test ID="1" Description="test1" Date="16/01/2023"></Test>
</Sim>
Try with XDocument and LINQ
var xDoc = XDocument.Parse(File.ReadAllText("XMLFile2.xml"));
var elements = xDoc.Descendants("Test").OrderByDescending(x=> Convert.ToInt16(x.Attribute("ID").Value));
I have an XML as below
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns="http://com/uhg/uht/uhtSoapMsg_V1"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
<uhtHeader
xmlns="http://com/uhg/uht/uhtHeader_V1">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="http://com/uhg/uht/getClaimHistory_V24">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I want to remove all the attribute values with "http" like below:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns=""
xmlns:env="">
<env:Header>
<uhtHeader
xmlns="">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I have tried several ways but none of them has worked for me. Can anyone suggest what is fastest way to do it in VB.NET/C#.
The actual response is very large (approx 100000 lines of XML minimum) and using for each will consume a good amount of time. Is there any parsing method or LINQ query method which can do it faster.
I got the way to do it using Regex as below:
Return Regex.Replace(xmlDoc, "((?<=<|<\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?="".*?""", "")
It serves my purpose completely. Thanks Cleptus for your quick reference.
I have to transform the XML DOM structure into another XML DOM structure, How can I perform it using custom serialization implemented via IXmlSerialization. The source and target XML structure are mentioned as below
Source XML
<?xml version="1.0"?>
<ConfigFileRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DeviceConnectionParams>
<DeviceConnectionParam>
<UserId>administrator</UserId>
<Password>B670AAB84F449BF3953C5ABE947DEF0C</Password>
<IsManual>True</IsManual>
</DeviceConnectionParam>
</DeviceConnectionParams>
<AlarmMappings>
<Enabled>true</Enabled>
<AlarmA>
<Events>
<EventId>
<PanelId>1</PanelId>
<PanelName>TestAccess</PanelName>
<DeviceId>65</DeviceId>
<DeviceName>TestPanel</DeviceName>
</EventId>
<EventId>
<PanelId>1</PanelId>
<PanelName>TestAccess</PanelName>
<DeviceId>65</DeviceId>
<DeviceName>TestPanel</DeviceName>
</EventId>
</Events>
</AlarmA>
<AlarmB>
<AlarmId>0a103f2b3ce7498c4fcaafb965c742cca66a221027054490eab5459689303bbea1f81898380c</AlarmId>
<NvrUuid>0a1027054490eab5459689303bbea1f818983801</NvrUuid>
</AlarmB>
</AlarmMappings>
</ConfigFileRoot>
Target XML
<?xml version="1.0"?>
<ConfigFileRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DeviceConnectionParams>
<DeviceConnectionParam>
<UserId>administrator</UserId>
<Password>B670AAB84F449BF3953C5ABE947DEF0C</Password>
<IsManual>True</IsManual>
</DeviceConnectionParam>
</DeviceConnectionParams>
<AlarmMappings>
<Enabled>true</Enabled>
<AlarmA>
<Events>
<EventId>
<PanelId>1</PanelId>
<PanelName>TestAccess</PanelName>
<DeviceId>65</DeviceId>
<DeviceName>TestPanel</DeviceName>
</EventId>
</Events>
</AlarmA>
<AlarmB> <AlarmId>0a103f2b3ce7498c4fcaafb965c742cca66a221027054490eab5459689303bbea1f81898380c</AlarmId>
<NvrUuid>0a1027054490eab5459689303bbea1f818983801</NvrUuid>
</AlarmB>
</AlarmMappings>
<AlarmMappings>
<Enabled>true</Enabled>
<AlarmA>
<Events>
<EventId>
<PanelId>1</PanelId>
<PanelName>TestAccess</PanelName>
<DeviceId>65</DeviceId>
<DeviceName>TestPanel</DeviceName>
</EventId>
</Events>
</AlarmA>
<AlarmB>
<AlarmId>0a103f2b3ce7498c4fcaafb965c742cca66a221027054490eab5459689303bbea1f81898380c</AlarmId>
<NvrUuid>0a1027054490eab5459689303bbea1f818983801</NvrUuid>
</AlarmB>
</AlarmMappings>
</ConfigFileRoot>
NEWBIE QUESTION.
I haven't worked that much with xml, nothing like this anyway. I have some XML as shown below that I receive which has several namespaces.
I need to read some values, then update others before returning the revised XML with namespaces intact - don't want them removed.
I am given the path to some of the elements like this cred/sub/aa or trip/items/item[0]/customerInfo/custName.
But it seems that namespaces make it difficult to get to those elements so simply.
Does anybody know how I can read some of the values like NON-SMOKING from custPref or get the value CABBAGE from bossman/zz.
Also, I want to be able to then set a value such as custName to say Mr. X.
Any ideas?
Thanks.
<?xml version="1.0" encoding="utf-16" ?>
<A1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<cred xmlns="https://blah-blah.com/?foobar">
<sub>
<aa>Zippo</aa>
<bb>lighter</bb>
</sub>
<reqId>
<cc></cc>
<dateOfBirth></dateOfBirth>
</reqId>
</cred>
<reqName xmlns="http://blah-blah/vader/base">qwerty</reqName>
<reqId xmlns="http://blah-blah/vader/base">12345</reqId>
<machine xmlns="http://blah-blah/vader/base">
<qqq>hello</qqq>
<www>goodbye</www>
<eee>99999</eee>
<rrr>88888</rrr>
</machine>
<monkey xmlns="http://blah-blah/vader/base">alskdjfhg</monkey>
<math xmlns="http://blah-blah/vader/base">
<language></language>
</math>
<trip xmlns="http://blah-blah/simple">
<tripOverview xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
</tripOverview>
<bossman xmlns="http://blah-blah/vader/base">
<zz>CABBAGE</zz>
<yy>BANANA</yy>
<xx>MELON</xx>
<ww>SYRUP</ww>
</bossman>
<items>
<item>
<itemSummary xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
<reference></reference>
</itemSummary>
<customerInfo xmlns="http://blah-blah/vader/base">
<custName></custName>
<custPref>NON-SMOKING</custPref>
</customerInfo>
<seatId xmlns="http://blah-blah/vader/base">1</seatId>
</item>
</items>
</trip>
</A1>
string xml = "<Root><Options></Options></Root>";
var xdocs = XDocument.Parse(xml);
xdocs.Descendants().Where(q => q.Name == "Options").FirstOrDefault().Value = "FoundIt";
The existing Project loads the XMLDocument from a string transactionXML which is like
\n\t<Transaction>
\n\t\t<TransactionId>6109</TransactionId>
n\t\t<TransactionType>ClaimsCorrespondenceEvent
</TransactionType>\n\t\t<TransactionStatus>
The below code loads xml sucessfully:
document.LoadXml(transactionXML);
but the below code fails and gives exception of invalid character referring to the \n\t etc.
XDocument.Load(transactionXML);
I need to use XDocument as I am using the method Descendants of XDocument which is not available in XMLDocument.
Please is there a way to load the above string by using XDocument. Alternatively how do I use XMLdocument to get all the nodes along with the child with node name "Transaction"
The example XML would be :
<Transactions>
<Transaction>
<Version> 1 </Version>
<Id> 2 </Id>
</Transaction>
<Transaction>
<Version> 2 </Version>
<Id> 3 </Id>
</Transaction>
<Transaction>
<Version> 3 </Version>
<Id> 4 </Id>
</Transaction>
</Transactions>
and I want separate XML for the node Transaction along with its child elements like :
Ist XML set
<Transaction>
<Version> 1 </Version>
<Id> 2 </Id>
</Transaction>
2nd XMl set
<Transaction>
<Version> 2 </Version>
<Id> 3 </Id>
</Transaction>
3rd XMl set
<Transaction>
<Version> 3 </Version>
<Id> 4 </Id>
</Transaction>
Please if some one could let me know how to go about.
For XDcoument, to load xml from a string, you need to use Parse not Load; Load accepts a file path and not an xml string, try:
var xml = XDocument.Parse(transactionXML);
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx