How to get the attribute value of an xml code using linq - c#-4.0

<Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2">
....</Shape>
<Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5">
....</Shape>
I have to return the Master value for every ID value.
How can i achieve it by using LINQ to XMl.

You didn't really present how your XML document looks like, so I assumed it's as follow:
<Shapes>
<Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2">
</Shape>
<Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5">
</Shape>
</Shapes>
You can simply get Master attribute value for all different ID like that:
var xDoc = XDocument.Load("Input.xml");
var masters = xDoc.Root
.Elements("Shape")
.ToDictionary(
x => (int)x.Attribute("ID"),
x => (int)x.Attribute("Master")
);
masters will be Dictionary<int, int> where key is your ID and value is corresponding Master attribute value.

Related

how to iterate xml nodes with groovy

I'm trying to iterate through an xml file with groovy to get some values.
I found many people with the same problem, but the solution they used doesn't work for me, or it's too complicated.
I'm not a groovy dev, so I need a bullet proof solution which I can implement.
Basically I have an xml response file that looks like this: ( it looks bad but that's what I get)
<Body>
<head>
<Details>
<items>
<item>
<AttrName>City</AttrName>
<AttrValue>Rome</AttrValue>
</item>
<item>
<AttrName>Street</AttrName>
<AttrValue>Via_del_Corso</AttrValue>
</item>
<item>
<AttrName>Number</AttrName>
<AttrValue>34</AttrValue>
</item>
</items>
</Details>
</head>
</Body>
I've already tried this solution I found here on StackOverflow to print the values:
def envelope = new XmlSlurper().parseText("the xml above")
envelope.Body.head.Details.items.item.each(item -> println( "${tag.name}") item.children().each {tag -> println( " ${tag.name()}: ${tag.text()}")} }
the best I get is
ConsoleScript11$_run_closure1$_closure2#2bfec433
ConsoleScript11$_run_closure1$_closure2#70eb8de3
ConsoleScript11$_run_closure1$_closure2#7c0da10
Result: CityRomeStreetVia_del_CorsoNumber34
I can also remove everything after the first println, and anything inside it, the result is the same
My main goal here is not to print the values but to extrapolate those values from the xml and save them as string variables...
I know that using strings is not the best practice but I just need to understand now.
Your code as is had 2 flaws:
with envelope.Body you would NOT find anything
if you fix No. 1, you would run into multiple compile errors for each(item -> println( "${tag.name}"). Here the ( is used instead of { and you use an undefined tag variable here.
The working code would look like:
import groovy.xml.*
def xmlBody = new XmlSlurper().parseText '''\
<Body>
 <head>
  <Details>
<items>
<item>
<AttrName>City</AttrName>
<AttrValue>Rome</AttrValue>
</item>
<item>
<AttrName>Street</AttrName>
<AttrValue>Via_del_Corso</AttrValue>
</item>
<item>
<AttrName>Number</AttrName>
<AttrValue>34</AttrValue>
</item>
</items>
 
  </Details>
 </head>
</Body>'''
xmlBody.head.Details.items.item.children().each {tag ->
println( "  ${tag.name()}: ${tag.text()}")
}
and print:
AttrName: City
AttrValue: Rome
AttrName: Street
AttrValue: Via_del_Corso
AttrName: Number
AttrValue: 34

Counting an XML attribute in Groovy

So I'm making a script in Groovy that parses a really large XML file, appends some stuff and slightly changes each element every time it appends. Each of these elements has an ID number associated with it and I want to make it so that every time an element is appended, the ID number will = the highest ID number in the file +1. I'll show a little piece of code to that will help understand:
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="xyxy" version="1.1">
<file original="zzz.js" source-language="en" target-language="en" datatype="javascript">
<body>
<trans-unit id="20" resname="foo">
<source>foofoo</source>
<target>foofoo</target>
</trans-unit>
<trans-unit id="21" resname="blah">
<source>blahblah</source>
<target>blahblah</target>
</trans-unit>
</body>
</file>
</xliff>
In this case, if I added an element (trans-unit) to the list, the ID would need to be 22. I have an algorithm that parses and appends, but I'm not sure how to increment the ID each time. Again, I'm using Groovy to do this. Does anyone have an idea? Thanks in advance!!
Assuming you have parsed that XML with xmlslurper or xmlparser, you should be able to get the next id with the help of max:
def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="xyxy" version="1.1">
<file original="zzz.js" source-language="en" target-language="en" datatype="javascript">
<body>
<trans-unit id="20" resname="foo">
<source>foofoo</source>
<target>foofoo</target>
</trans-unit>
<trans-unit id="21" resname="blah">
<source>blahblah</source>
<target>blahblah</target>
</trans-unit>
</body>
</file>
</xliff>'''
def x = new XmlSlurper().parseText(xml)
def next = 1 + x.file.body.'trans-unit'*.#id*.text().collect { it as Integer }.max()
assert next == 22
To use XmlParser, you need to change the line to:
def next = 1 + x.file.body.'trans-unit'*.#id.collect { it as Integer }.max()

CRM 2011 FetchXml to pull list accounts and display only most recent activity dates

I am trying to create a report in fetchxml on crm 2011 that displays a list of Account Names, and also in the same row displays the date of ONLY the most recent activity for that account.
So
Report should look like
Account1, Date of most recent activity for Account 1
Account2, Date of most recent activity for Account 2
Account3, Date of most recent activity for Account 3
I have a fetch query that pulls the correct data, but it pulls a row for each activity for the account instead of just for the newest activity.
so it looks like
Account1, Date of most recent activity for Account 1
Account1, Date of other activity for Account 1
Account2, Date of most recent activity for Account 2
Account2, Date of other activity for Account 2
Account3, Date of most recent activity for Account 3
Here is my fetch
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="false">
<entity name="<accountentityname>"
<attribute name="<accountname>" />
<link-entity name="activity" from="<regardingaccoutnfield>" to="<account field>" visible="false" link-type="outer">
<attribute name="<date of activity>" />
</link-entity>
</entity>
</fetch>
Any suggestions?
Thanks.
Not sure if the maxaggregate attribute will work with datetimes, but if it does that might work
What you're wanting to do requires a subquery. This is something that CRM does not support.
If you're not using CRM Online, just perform a standard SSRS query using SQL rather then FetchXml.
If you're just trying to pull back data with the SDK, you could perform the subquery on the client side.
Why Max Aggregate Won't Work
If your column you were wanting to get the max value on was a number you could use an aggregate, but Aggregate functions AVG, MIN, MAX, or SUM can only be applied to attributes of type integer, float, money, bigint, or decimal.
That's the error if you attempt to run this query:
var xml = #"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true'>
<entity name='contact'>
<attribute name='fullname' alias='name' groupby='true' />
<attribute name='contactid' alias='id' groupby='true' />
<link-entity name='activitypointer' from='regardingobjectid' to='contactid' alias='ad' link-type='outer'>
<attribute name='createdon' alias='createdon_max' aggregate='max' />
<filter type='and'>
<condition attribute='createdon' operator='not-null' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection fetchResult = service.RetrieveMultiple(new FetchExpression(xml);
You can do a sort on the date and return the first record only!
Do your query on accounts then return a for each & use something like:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="activitypointer">
<attribute name="scheduledstart" />
<order attribute="scheduledstart" descending="true" />
<filter type="and">
<condition attribute="scheduledstart" operator="not-null" />
</filter>
</entity>
</fetch>
OR similar Query Expression:
QueryExpression query = new QueryExpression("activitypointer");
query.ColumnSet.AddColumns("scheduledstart");
query.AddOrder("scheduledstart", OrderType.Descending);
EntityCollection results = service.RetrieveMultiple(query);
Then something like:
(DateTime)(((Entity)results.Entities.First()).Attributes["scheduledstart"]);
Add the condition that the regarding field is equal to the account.id that's in scope for the for each loop

JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?

I have the following FXML:
<ChoiceBox>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
But in the GUI it just shows a ChoiceBox with a default of nothing. I would like the first element in the list to be the default, and for a choice of "null" or nothing to be prohibited.
How do I accomplish this?
I added the value attribute to the ChoiceBox tag, and that worked.
<ChoiceBox value="2 minutes">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
First, you should import your needed value model, like Crowell answer, you should import like this in your fxml header:
<?import javafx.collections.*?>
Second, if you want's import your own model, import it first and then like this:
<?import com.zzg.mybatis.generator.model.*?>
....
<ChoiceBox layoutX="24.0" layoutY="14.0" prefWidth="150.0">
<items>
<FXCollections fx:factory="observableArrayList">
<DatabaseDTO name="MySQL" value="1"></DatabaseDTO>
<DatabaseDTO name="Oracle" value="2"></DatabaseDTO>
</FXCollections>
</items>
</ChoiceBox>
#Groostav: In case we programmatically "know" the value that should appear as selected (for example, we landed in an edit form), we can do the following:
1) Add a new item with index 0 (that is, the element we need to show as selected):
myChoiceBox.getItems().add(0, ItemObtainedProgrammatically);
2) Show the item as selected (since we already know it's at position 0):
myChoiceBox.getSelectionModel().select(0);
Probably this qualifies as a dirty hack, but it works. The con: You have the same item twice in your choicebox

CopyIntoItems Sharepoint SOAP Action Parameters

I am trying to upload a file using SOAP Action
POST /_vti_bin/Copy.asmx HTTP/1.1
Host: my.hostname.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<SourceUrl>string</SourceUrl>
<DestinationUrls>
<string>string</string>
<string>string</string>
</DestinationUrls>
<Fields>
<FieldInformation Type="Invalid or Integer or Text or Note or DateTime or Counter or Choice or Lookup or Boolean or Number or Currency or URL or Computed or Threading or Guid or MultiChoice or GridChoice or Calculated or File or Attachments or User or Recurrence or CrossProjectLink or ModStat or AllDayEvent or Error" DisplayName="string" InternalName="string" Id="guid" Value="string" />
<FieldInformation Type="Invalid or Integer or Text or Note or DateTime or Counter or Choice or Lookup or Boolean or Number or Currency or URL or Computed or Threading or Guid or MultiChoice or GridChoice or Calculated or File or Attachments or User or Recurrence or CrossProjectLink or ModStat or AllDayEvent or Error" DisplayName="string" InternalName="string" Id="guid" Value="string" />
</Fields>
<Stream>base64Binary</Stream>
</CopyIntoItems>
</soap:Body>
</soap:Envelope>
I wish to upload a PDF file or Doc file. I am using Firefox extension Poster
I have the source and destination URLs
What should be the values of <FieldInformation> and <Stream> ?
Thanks in advance
Viv
The stream value needs to be the base64binary representation of the file. So for example I have a text file that contains the text "123". Using this site http://www.base64converter.com/ I converted the text to base64 which translated to "MTIz". So if I was trying to upload my file I would put "MTIz" (without quotes) into the Stream tag. Something like this:
<Stream>MTIz</Stream>
The FieldInformation tags need to contain any SharePoint properties that need to be associated with the document you are uploading. So if you want to set the title property to "My Document" you would need something like this:
<FieldInformation Type="Text" DisplayName="Title" InternalName="Title" Id="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Value="My Document" />

Resources