How to Display linqxml list data to datatable - linq

I have an xml like this
<?xml version="1.0" encoding="utf-8"?>
<content>
<field title="Year">
<description>Numeric data</description>
<comment>1234</comment>
</field>
<field title="mail">
<description>Numeric data</description>
<comment>ABCD</comment>
</field>
<field title="Year">
<description>AlphNumeric Data</description>
<comment>ABCD1234</comment>
</field>
</content>
i tried below linq query to extract data based on the title attribute.
var Data = XDocument.Load(Xmlpath).Root
.Descendants("field")
.Where(element => element.Attribute("title").Value.Contains("Year"))
.Descendants()
.Where(element => element.Name == "description" || element.Name == "comment")
.Select(element => new { element.Name, element.Value }).ToList();
for eg i want to show all datas that comes under title "year", the output should be displayed into a datatable like this
Description Comment
numeric data 1234
Alphanumeric Data ABCD1234
But i dont know how to convert var data to datatable or iterarte though var data .Can somebody help me? is it possible to display the result from linq query by modifying the query itself?

DataTable's have a method (ReadXml) that reads XML. Have you tried using that?

Related

Odoo duplicate many2many fields

A model [m] with a many2many field [m2m]
Field [m2m] have attribute [copy=False]
A form view [v] display the [m] and the field [m2m] with widget [many2many_tags]
I have tested that the python object did return a with False on field [m2m]
but the javascript, keep render the tags on web.
model.py
class model(models.Model):
_name = "m"
m2m = fields.Many2many('sale.order', string="SO", copy=False)
XML File
<record ...>
<form>
<group>
<field name="name"/>
<field name="ref"/>
<field name="so_m2m" widget="many2many_tags" options="{'no_create_edit': True}"/>
</group>
</form>
</record>
When I duplicate the current record, the field [m2m] is keeps the old tag from old record which is not expected.
After clicking the save button, the old tag will disappear.
Try one thing here :
Whatever your third table is there, both fields used in many2many relation, from third table keep them as a copy false.
i.e.
m2m = fields.Many2many('sale.order','sale_order_another_table_rel', 'sale_order_field','antother_table_field', string="SO", copy=False)
Into new table :
_name = 'sale.order.another.table.rel'
sale_order_field_id = // copy=false
another_table_field_id = //copy = false
Hopefully work.

Xpath - Extract an email address from a long string but excluding the rest of the sting

I use xpath. I want to match the email only (and not the entire string) using xpath.
So say the string is:
<p> You can email me at andy.pandy#gmail.com </p>
Most of the answers here cite something on the lines of //p[contains(., '#')]
However, this will return the entire text within the <p> tag. I want to only return andy.pandy#gmail.com excluding the rest.
Is this possible using xpath?
Assuming your XML looks something like this:
<?xml version='1.0' encoding='UTF-8'?>
<data>
<p>notanemail</p>
<p>email#example.com</p>
<p>nothing</p>
</data>
You can call:
XElement xmlData = XElement.Load("mails.xml");
var emailList = xmlData.XPathSelectElements(#"//p[contains(., '#')]")
.Select(p => p.Value)
.ToList();

Odoo 8 strange behavious using new api to inherit old api model

I added two fields to stock.picking using new api:
class StockPicking(models.Model):
_inherit = "stock.picking"
address = fields.Char(related='partner_id.street', string="Address")
sector = fields.Char(related="partner_id.sector", string="Sector")
Then I added onchange method to load stock pickings in One2Many field defined using tree:
<field name="line_ids" >
<tree>
<field name="name" />
<field name="partner_id" />
<field name="address"/>
<field name="sector"/>
<field name="state" />
</tree>
</field>
I used a simple domain with search [('id', '<', '10')] then I changed line_ids value but no line was loaded in the web view.
After many times trying to find a log error with no success I changed the code from the new API to the old API:
class StockPicking(osv.osv):
_inherit = "stock.picking"
_columns = {
'address': fields.related('partner_id', 'street', type='char', relation='res.partner', string='Address'),
'sector': fields.related('partner_id', 'sector', type='many2one', relation='sector', string='Sector')
}
For a reason that I do not know It worked.
Did someone know why this did not work using the new API?

Unable to use related field in domain filter

Field declaration in odoo v8, team_manager is a new class:
class team_manager(osv.osv):
_name = "team.manager"
_columns = {
'is_manager': fields.related('manager_id', 'manager', type='boolean', relation="hr.employee", string='Managers', readonly=True, store=True)
}
The xml file code for 'Form View'
<separator string="Team Work" attrs="{'invisible':[('is_manager','=',False)]}"/>
<field name="child_line" context="{'manager_id':id}" attrs="{'invisible':[('is_manager','=',False)]}">
<tree editable="=top">
<field name="employee_id"/>...
</tree>
</field>
While opening the form I am getting the error like
Odoo Client Error
Error: Unknown field is_manager in domain [["is_manager","=",false]]
http://localhost:8069/web/static/src/js/view_form.js:1702
Can't we use related filed in domain? or Does my syntax need changes?
You need to add is_manager field to the form view.
Add the following line before child_line field:
<field name="is_manager" invisible="True"/>

How do you add fields to com_content in Joomla! with a plugin and store the data in its own table?

I'm running Joomla 1.7 and I know that it has the ability to add custom form fields to components with a plugin.
There is a sample plugin located at:
/plugins/user/profile
This plugin allows you to put custom form fields on the user profile front end and back end and these fields are stored in a custom table.
I created a similar plugin for user profiles and it worked perfectly.
However, when I go to create a plugin like this for com_content, I am met with a problem.
this is what my XML file looks like:
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="additionalinfo">
<fieldset name="additionalinfo_fieldset" label="PLG_CONTENT_ADDITIONALINFO_FIELDSET_LABEL">
<field name="tagline" type="text" size="50" default="" label="PLG_CONTENT_ADDITIONALINFO_TAGLINE_LABEL" description="PLG_CONTENT_ADDITIONALINFO_TAGLINE_DESC" />
<field name="pseudocategory" type="category" extension="com_content" label="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_LABEL" description="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_DESC" />
</fieldset>
</fields>
</form>
This however does not work, whenever I do something like above, the form fields never show up on the admin form (even though I have it set correctly, and the only thing that changed between the user plugin and the content plugin is the name of the form i'd like the form to appear on
When I change my XML to this:
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="attribs">
<fieldset name="additionalinfo_fieldset" label="PLG_CONTENT_ADDITIONALINFO_FIELDSET_LABEL">
<field name="tagline" type="text" size="50" default="" label="PLG_CONTENT_ADDITIONALINFO_TAGLINE_LABEL" description="PLG_CONTENT_ADDITIONALINFO_TAGLINE_DESC" />
<field name="pseudocategory" type="category" extension="com_content" label="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_LABEL" description="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_DESC" />
</fieldset>
</fields>
</form>
When I make this simple change, the form fields show up! BUT, the data isn't stored or retrieved from the custom table, it just goes into the 'attribs' column on the _content table. This stores the content in JSON, which is alright, but we'd like to be able to index the content by the custom fields (and not have to loop through each record in the database to find what we're looking for).
Any ideas on how to fix this?
thanks!
david barratt
I guess your plugin file ( for example, "yourplugin.php" ) will have one method called "onContentPrepareForm". If you want to add data to an article, this method should start like this:
function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
// Check we're manipulating an
if ( $form->getName() != "com_content.article" ) {
return true;
}
//[...] The rest of your code here
Besides, if you want to store these fields in another table in order to make it easier to search using this fields, maybe you should create a new table and save the data using the "onContentAfterSave" method:
public function onContentAfterSave( $context, &$article, $isNew )
On this method, you should always check that $context is "com_content.article", otherwise you might face problems when saving categories.
I hope it helps!

Resources