I have a large xml file and want to get a defined number of <Cooperation> nodes from it. What's the best way to handle this.
Currently, I'm using this code
public string FullCooperationListChunkGet(int part, int chunksize)
{
StringBuilder output_xml = new StringBuilder();
IEnumerable<XElement> childList = from el in xml.Elements("Cooperations").Skip(part * chunksize).Take(chunksize) select el;
foreach (XElement x in childList.Elements())
{
output_xml.Append(x.ToString());
}
return output_xml.ToString();
}
Skip(part * chunksize).Take(chunksize) doesn't work (seems to be only valid for the Cooperations Tag and not the Cooperation Tags)
Can somebody point me in the right direction.
Thanks,
rAyt
Edit:
The Background is this: I'm pushing these xml parts via a webservice to a Blackberry. Unfortunately, the http request size on a blackberry enterprise server is limited
to 256 kb by default.
Part of the XML File:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>xxx</CooperationName>
<LogicalCustomers>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx/CustomerName>
</Customer>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
</LogicalCustomers>
</Cooperation>
<Cooperation>
...
For using XDocument, I expect you want something like:
var qry = doc.Root.Elements("Cooperation").Skip(part*chunksize).Take(chunksize);
however, if the data is large, you might have to drop down to XmlReader instead... I'll try to do an example... (update; 512kb probably isn't worth it...)
The problem with your code is that you are using .Elements() here:
foreach (XElement x in childList.Elements())
{
output_xml.Append(x.ToString());
}
Just remove that:
foreach (XElement x in childList)
{
output_xml.Append(x.ToString());
}
For info - you are also using query syntax unnecessarily:
IEnumerable<XElement> childList = from el in xml.Elements("Cooperations")
.Skip(part * chunksize).Take(chunksize) select el;
is 100% identical to:
IEnumerable<XElement> childList = xml.Elements("Cooperations")
.Skip(part * chunksize).Take(chunksize);
(since the compiler ignores an obvious select, without mapping it to the Select LINQ method)
Do you have an xml document or a fragment, i.e do you have more than 1 "Cooperations" nodes? If you have more, which Coopertation's are you expecting to get? From just 1 Cooperations or across multiple, reason for asking is that you have written xml.Elements("Cooperations").
Wouldn't this do the trick:
xml.Element("Cooperations").Elements("Cooperation").Skip(...).Take(...)
You could do this by using System.Net instead of LINQ, although it would be quite messy. Just to give you an idea on how you can read parts of a http response:
// Get the HTTP response
string url = "http://someurl.com/myxml.xml";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Build a stream
Stream stream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader( stream, encode );
// Loop the file
Char[] read = new Char[256];
int count = reader.Read( read, 0, 256 );
while (count > 0) {
String str = new String(read, 0, count);
count = reader.Read(read, 0, 256);
}
response.Close();
stream.Close();
You can use paging by adjusting the count and simultaneously searching str for XML tags.
Related
I'm currently trying to retrieve data from a web service, I would like to do search on results if the score is over 90 for example. I have tried to bring back results without doing searches also and getting no results. Could someone please give me a hand in where I'm going wrong?
FundNamesPayload xmlresponse = new FundNamesPayload();
xmlresponse = search.SearchByName("Australiansuper", "GUID-Here", "Y");
MemoryStream XmlStream = new MemoryStream();
StreamReader XmlReader = new StreamReader(XmlStream);
XmlSerializer Serializer = new XmlSerializer(typeof(FundNamesPayload));
Serializer.Serialize(XmlStream, xmlresponse);
XmlStream.Seek(0, System.IO.SeekOrigin.Begin);
var str = XElement.Parse(XmlReader.ReadToEnd());
var Matching = from data in str.Descendants("FundName")
where(int)data.Element("Score") > 90
select data;
Here is an example of the XML
<SuperFundNamesPayload xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://superfundlookup.gov.au">
<Request>
<Guid>************</Guid>
<Name>HOST Plus</Name>
<ActiveFundsOnly>Y</ActiveFundsOnly>
</Request>
<Response>
<DateTimeRetrieved>2017-09-25T12:20:40.8446457+10:00</DateTimeRetrieved>
<MatchingFundNames>
<NumberOfRecords>2</NumberOfRecords>
<MatchingFundName>
<ABN>
<Value>68657495890</Value>
<IdentifierStatus>Active</IdentifierStatus>
</ABN>
<FundName>
<Name>THE TRUSTEE FOR HOST PLUS SUPERANNUATION FUND</Name>
<NameType>Entity Name</NameType>
<Score>94</Score>
<NameStatus>Current</NameStatus>
</FundName>
<Location>
<StateTerritoryCode>VIC</StateTerritoryCode>
<Postcode>3000</Postcode>
</Location>
</MatchingFundName>
<MatchingFundName>
<ABN>
<Value>80567702967</Value>
<IdentifierStatus>Active</IdentifierStatus>
</ABN>
<FundName>
<Name>The Trustee for HOIST HYDRAULICS VIC P L SUPER FUND</Name>
<NameType>Entity Name</NameType>
<Score>73</Score>
<NameStatus>Current</NameStatus>
</FundName>
<Location>
<StateTerritoryCode>VIC</StateTerritoryCode>
<Postcode>3137</Postcode>
</Location>
</MatchingFundName>
</MatchingFundNames>
</Response>
</SuperFundNamesPayload>
The problem is that the XML document specifies a default namespace:
<SuperFundNamesPayload ... xmlns="http://superfundlookup.gov.au">
So you must specify that namespace when you look up elements:
XNamespace ns = "http://superfundlookup.gov.au";
var Matching = from data in str.Descendants(ns + "FundName")
where (int)data.Element(ns + "Score") > 90
select data;
There a couple atypical features of the LINQ to XML syntax:
You create an XNamespace object by using its implicit string conversion operator instead of new.
You create a namespace-qualified name (an XName) by concatenating an XNamespace object and a string with the + operator.
I had a similar issue because I have forgot to write the constructor (the 'var xmlresponse = new FundNamesPayload();' line). I've got no exception, no error, just an empty (null) result..
I am developing a universal windows app on windows 10 with Visual Studio 2015 and have a pretty large Xml structured like this:
<header id = "1">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
<header id = "2">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
...
This is repeating many times. There are parts that should never be changed (e.g. title, question). Now i want to write new elements into "ui", so it can be read again and shows the new content in texbox.
I use a FileStream and XmlDocument and XmlNodeList to read the Xml and show the content on textblocks:
path = "test.xml";
FileStream stream = new Filestream(path, FileMode.Open, FileAcces.Read);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(reader);
XmlNodeList node = xdoc.GetElementsByTagName("header");
textblock1.Text = node[0].Attributes["id"].Value;
textblock2.Text = node[i].ChildNode[1].InnerText;
....
I tried this to write into the Xml:
XDocument xdoc = XDocument.Load(path);
XElement ele = xdoc.Element("header");
ele.Add(new XElement("user_input",
new XElement("input1", newtext)));
xdoc.Save(path); <---- at this point there is an error
"Argument 1: cannot convert from 'string' to 'System.IO.Stream'"
My question is: how can i write the user input (some string) to the place I want it to be? The first input shall be written into header with id = 1 into user_input, the second into header id = "2" and so on. I already tried to load the xml with XDocument and write a new element with XElement, but it work at all.Is there something wrong with my xml? Or is it the function? Thank you in advance.
Firstly, the xml file cannot contain same roots, here you have two headers nodes but don't see a root node. So I add a root node for testing your xml file as follows
<?xml version="1.0" encoding="utf-8"?>
<Topics>
<header id = "1">
...
</header>
</Topics>
Secondly, this error
"Argument 1: cannot convert from 'string' to 'System.IO.Stream'"
xdoc.save(string) is not available in uwp, details you can see the version information of XDocument.Save method.
Thirdly, for this question
how can i write the user input (some string) to the place I want it to be?
we can insert value to special element by xpath or GetElementsByTagName method. In uwp, I recommend you use Windows.Data.Xml.Dom namespace instead of System.xml.Ling.
Here I wrote a demo for insert value to special place . And upload the demo to GitHub, you can download CXml for testing.
Mainly Code
private async void BtnXmlWrite_Click(object sender, RoutedEventArgs e)
{
String input1value = TxtInput.Text;
if (null != input1value && "" != input1value)
{
var value = doc.CreateTextNode(input1value);
//find input1 tag in header where id=1
var xpath = "//header[#id='1']/user_input/input1";
var input1nodes = doc.SelectNodes(xpath);
for (uint index = 0; index < input1nodes.Length; index++)
{
input1nodes.Item(index).AppendChild(value);
}
RichEditBoxSetMsg(ShowXMLResult, doc.GetXml(), true);
}
else
{
await new Windows.UI.Popups.MessageDialog("Please type in content in the box firstly.").ShowAsync();
}
}
More details you can reference XML dom Sample, XML and XPath.
So I am trying to parse through some XML which is being returned from a REST API call. The XML looks like this (with many more <link>'s of course):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response status="Ok">
<links>
<link id="79380" hint="Some Text" linkDescription="This is the GET url for this Customer." link="/customer/79380" httpMethod="GET"/>
</links>
</response>
I am loading the XML file using the following code:
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
XElement doc = XElement.Load(reader);
I then loop through the elements like so:
IEnumerable<XElement> List =
from el in doc.Descendants("links") select el;
foreach (XElement e in List)
{
test += e.ToString();
}
It only loops through once and test is just a string that contains the entire XML file. My goal is to get the value of the "id" attribute from each element and place them in a list.
I have tried various things and I can't seem to get anything back but one huge string.
Try this:
var idList = doc.Descendants("link").Select(x => (int)x.Attribute("id"));
I have a response from web service in SOAP envelope as follows:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ProcessTranResponse xmlns="http://www.polaris.co.uk/XRTEService/2009/03/">
<ProcessTranResult xmlns:a="http://schemas.datacontract.org/2004/07/XRTEService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:PrintFormFileNameContents i:nil="true"/>
<a:ResponseXML>response_message</a:ResponseXML>
</ProcessTranResult>
</ProcessTranResponse>
</s:Body>
And I want to get response_message to a string variable. I tried doing
XDocument doc = XDocument.Parse(Response);
XNamespace xmlnsa = "http://schemas.datacontract.org/2004/07/XRTEService";
var ResponseXML = doc.Descendants(xmlnsa + "ResponseXML");
And when I use watch I see in ResponseXML -> Results View[0] -> Value my response_message, but I can't figure out what is the next step to get to Value from C#.
XContainer.Descendants returns a collection of elements. You should then try something like this:
foreach (XElement el in ResponseXML)
{
Console.WriteLine(el.Value);
}
Or you can do something like this if you know that there is always only one response:
XDocument doc = XDocument.Parse(Response);
XNamespace xmlnsa = "http://schemas.datacontract.org/2004/07/XRTEService";
XElement ResponseXML = (from xml in XMLDoc.Descendants(xmlnsa + "ResponseXML")
select xml).FirstOrDefault();
string ResponseAsString = ResponseXML.Value;
You can employ several solutions to meet your purpose while you may wanna introduce the structure of xml content or not.
Static attitude
You can simply use this:
XmlDocument _doc = new XmlDocument();
doc.LoadXml(_stream.ReadToEnd());
Then find the desired data with something like this:
doc.LastChild.FirstChild.FirstChild.LastChild.InnerText;
Read xml structure
You can write some additional lines of code to introduce namespaces and other xml contents to find/map the available data, by taking a look at extracting-data-from-a-complex-xml-with-linq
I'm trying to deserialize the following XML output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Foo>
<Val>Data1</Val>
</Foo>
<Foo>
<Val>Data2</Val>
</Foo>
(This is output from a hardware device, and cannot be changed)
I have an XML type defined as:
[XmlType(AnonymousType=true, Namespace="")]
public class Foo
{
public string Val { get; set; }
}
I've tried to deserialize this array by creating a serializer like:
var s = new XmlSerializer(typeof(Foo[]));
//or
var s = new XmlSerializer(typeof(List<Foo>);
But every call to s.Deserialize() causes an InvalidOperaitonException:
System.InvalidOperationException: <Foo xmlns=''> was not expected.
Note
var s = new XmlSerializer(typeof(Foo));
// Only deseralizes the first Foo (Data1).
Thanks for your help.
I think the issue is with your provided xml.
Test app says
List<Foo> list = new List<Foo> {new Foo {Val = "Data1"}, new Foo {Val = "Data2"}};
var s = new XmlSerializer(typeof(List<Foo>));
StringBuilder sb = new StringBuilder();
XmlWriter wr = XmlWriter.Create(sb);
s.Serialize(wr, list);
string ss = sb.ToString();
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(ss);
List<Foo> returnList = (List<Foo>)s2.Deserialize(sr);
And the XML should be
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfFoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Foo>
<Val>Data1</Val>
</Foo>
<Foo>
<Val>Data2</Val>
</Foo>
</ArrayOfFoo>
If you can remove the inital line
<?xml version="1.0" encoding="ISO-8859-1"?>
And minipulate the string into
string s = "<ArrayOfFoo><Foo> <Val>Data1</Val></Foo><Foo> <Val>Data2</Val></Foo></ArrayOfFoo>";
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(s);
List<Foo> list = (List<Foo>)s2.Deserialize(sr);
That could work.
That isn't valid Xml. There needs to be a core root element for it to work properly.
this is not a valid xml so you can not deserialize it like a valid xml. You need some kind of hack to make this work. i'd suggest to insert at beginning of the xml and inserting at the end of xml. then you can deserialize it, since you cant make this change at xml side, do it in your code.
String ss;
// lets assume this holds your xml data in string.
ss.append("</ArrayOfFoo>");
ss.replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "<?xml version=\"1.0\" encoding=\"utf-16\"?> <ArrayOfFoo>")
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(ss);
List<Foo> returnList = (List<Foo>)s2.Deserialize(sr);
now this shall return you the correct list.
As the other posters say, this XML that the hardware device produces is not compatible to the way .NET serializes/deserializes object. Valid XML, and .NET requires valid XML, has a root element.
I suggest:
either you modify your obtained XML to match the way astander presents in his xml code snippet.
or you write a simple xml parser for your file that deserializes the file like you need
br, Marcel
Technically, what you have there is not a well-formed XML document (which has exactly one root element), but rather an well-formed external parsed entity (which can have any number of elements not contained in other elements, as well as text not contained in any elements). Therefore, it should parse if your XML parser has an entry point for parsing EPEs rather than documents. You could also create a stub document which includes by reference your EPE and parse that document.
Xstream for .Net could be a useful API