Asp.net vb find XML nodes - asp.net

i have the below xml and i want to get the phone from each item.
I have a loop where i can read the name and surname with success but i'm stacked on the other part.
Which is the xpath i should follow
<items>
<item>
<name></name>
<surname></surname>
<list>
<phone></phone>
<email></email>
</list>
</item>
<item>
<name></name>
<surname></surname>
<list>
<phone></phone>
<email></email>
</list>
</item>
</items>
Thanks

dom.xpath("//item").each do |item_node|
name = item_node.xpath("./name")
surname = item_node.xpath("./surname")
phone = item_node.xpath("./list/phone")
email = item_node.xpath("./list/email")
end

Related

Select node by attribute name through XSL in ASP.NET

I'm trying to select a node by its attribute name, but my selectors return nothing.
XML
<?xml version="1.0" encoding="utf-8"?>
<products>
<product ID="10036218">
<name>TEST</name>
<price currency="EUR">8.95</price>
<categories>
<category path="Feestartikelen">Feestartikelen</category>
</categories>
<properties>
<property name="fromPrice">
<value>8.95</value>
</property>
<property name="gender">
<value></value>
</property>
<property name="deliveryCosts">
<value>3.95</value>
</property>
<property name="model">
<value>Balloons</value>
</property>
</properties>
<variations/>
</product>
</products>
Dim nodeList As XmlNodeList = root.SelectNodes("/products/product")
nodelist.Count = 530 here, so I'm getting results.
Next I tried both properties/property/#model/value and properties/property/#model as values to select the node. Both return nothing.
For Each node In nodeList
If node.SelectSingleNode("properties/property/#model/value") IsNot Nothing Then
End If
Next node
What's wrong with my expressions?
This should get you the node by attribute name:
For Each node In nodeList
If node.SelectSingleNode("properties/property[#name='model']/value") IsNot Nothing Then
End If
Next node

QUiLoader and ignored dynamic properties

I'm loading the .ui file, where one of the widgets (QComboBox) has a dynamic property (http://qt-project.org/doc/qt-5/properties.html#dynamic-properties). The UI file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PopulateScriptConfig</class>
<widget class="QWidget" name="PopulateScriptConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="langGroup">
<property name="title">
<string>Language</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QComboBox" name="langCombo">
<property name="ScriptingLangCombo" stdset="0">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="codeGroup">
<property name="title">
<string>Implementation</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPlainTextEdit" name="codeEdit"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
The important part is:
<widget class="QComboBox" name="langCombo">
<property name="ScriptingLangCombo" stdset="0">
<bool>true</bool>
</property>
</widget>
I'm loading the file with QUiLoader::load(). I have extended the QUiLoader class, but only to access createWidget() method, where I can query each widget like this:
QWidget* UiLoader::createWidget(const QString& className, QWidget* parent, const QString& name)
{
QWidget* w = QUiLoader::createWidget(className, parent, name);
qDebug() << w->dynamicPropertyNames();
return w;
}
As a result I see empty list displayed, so it seems like the dynamic property is completly ignored.
Note, that UI editor in QtCreator recognizes this dynamic property correclty.
This question was already asked/reported 2 times before, but no solution was provided:
https://bugreports.qt-project.org/browse/QTBUG-11791?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab
http://www.qtcentre.org/threads/32013-QUiLoader-Problem-No-Dynamic-Properties-Support
Any ideas?
P.S. I've made sure that I load correct file. 3 times.
It turned out it was my mistake and Qt is working correctly. Rookie mistake.
The problem is that I tried to access dynamic properties in overriden method of QUiLoader::createWidget(), but this is invoked for each subwidget and dynamic properties are set after all widgets were created. Dynamic properties are not yet available at the moment of createWidget() call.
The very simple project I did for testing this (I mentioned it in the comment) was also working correctly, I just made a mistake while printing debug information (I was printing it from wrong widget). That's all.
QUiLoader works just fine with dynamic properties after all.

Spring MVC 3.2 - XStreamAlias Ignored

Person.java:
#XStreamAlias("person")
public class Person {
#XStreamAlias("id")
private Long personProfileId;
private String lastName;
private String firstName;
private String middleName;
private String nameSuffix;
private String namePrefix;
// etc ...
}
Spring configuration:
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="ignoreAcceptHeader" value="true" />
<property name="useJaf" value="false" />
<property name="defaultContentType" value="application/xml" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager" ref="contentNegotiationManager" />
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager" />
A call to http://mycompany.com:8080/myapp/persons?format=xml returns:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<com.example.myapp.model.Person>
<personProfileId>1</personProfileId>
<lastName>McCartney</lastName>
<firstName>James</firstName>
<middleName>Paul</middleName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
<personProfileId>2</personProfileId>
<lastName>Lennon</lastName>
<firstName>John</firstName>
<middleName>Winston</middleName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
<personProfileId>3</personProfileId>
<lastName>Starkey</lastName>
<firstName>Richard</firstName>
</com.example.myapp.model.Person>
<com.example.myapp.model.Person>
<personProfileId>4</personProfileId>
<lastName>Harrison</lastName>
<firstName>George</firstName>
</com.example.myapp.model.Person>
</list>
I would expect it to return:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<Person>
<id>1</id>
<lastName>McCartney</lastName>
<firstName>James</firstName>
<middleName>Paul</middleName>
</Person>
<Person>
<id>2</id>
<lastName>Lennon</lastName>
<firstName>John</firstName>
<middleName>Winston</middleName>
</Person>
<Person>
<id>3</id>
<lastName>Starkey</lastName>
<firstName>Richard</firstName>
</Person>
<Person>
<id>4</id>
<lastName>Harrison</lastName>
<firstName>George</firstName>
</Person>
</list>
It seems like XStream is correctly being called to marshall the object to XML, but that the #XStreamAlias annotations are being ignored. Is there some further configuration needed to get this to work?
I was trying to figure out this same thing (though with a Spring Boot project) and found an alternative: autodetectAnnotations.
xstreamMarshaller.setAutodetectAnnotations(true);
See this project, add the above line to the Application.java.
Figured it out. The annotated classes need to be explicitly identified to the XStreamMarshaller.
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="annotatedClasses">
<array>
<value>com.example.myapp.model.Person</value>
</array>
</property></bean>
</constructor-arg>
</bean>
</list>
</property>
As per XStream documentation https://x-stream.github.io/annotations-tutorial.html
you have to use xstream.processAnnotations(MyPOJO.class);

How to filter XML elements using LINQ query for one attribute/field containing a particular substring?

Example:
<Item name="item1">
<mode = "ax, bx" />
</Item>
<Item name="item2">
<mode = "bx, cx" />
</Item>
<Item name="item3">
<mode = "cx, dx" />
</Item>
In the example above I would like to extract all the items with modes containing "cx".
Thanks in advance!
Your XML in the example is not well formed. Assuming you meant:
<Items>
<Item name="item1">
<mode>ax, bx</mode>
</Item>
<Item name="item2">
<mode>bx, cx</mode>
</Item>
<Item name="item3">
<mode>cx, dx</mode>
</Item>
</Items>
you can do:
var els=from el in XDocument.Parse(inxml).Descendants("Item")
where el.Element("mode").Value.Contains("bx")
select el;
That is not legal XML (mode is an element name, you can set it equal to a string), but you should be able to do something like this, assuming that the string you are matching is the element value:
doc.Descendants("Item").Where( item => item.Elements("mode").Any( mode => mode.Value.Contains("cx")));
Assuming a well formed XML doc:
<Items>
<Item name="item1">
<mode>ax, bx</mode>
</Item>
<Item name="item2">
<mode>bx, cx</mode>
</Item>
<Item name="item3">
<mode>cx, dx</mode>
</Item>
</Items>
Do something like this:
XElement items = XElement.Load(#"C:\items.xml");
var filteredItems = from item in items.Descendants("Item")
where item.Element("mode").Value.Contains("cx")
select item;
foreach (var item in filteredItems)
{
Console.WriteLine(item.FirstAttribute.Value);
}
Output:
item2
item3

Mapping with BizTalk + Multiple nodes to a single one

I'm trying to make a mapping with this xml:
<rootxml>
<documents>
<document>
<iddoc>1</iddoc>
<total_price>1000</total_price>
</document>
</documents>
<items>
<item>
<iddoc>1</iddoc>
<iditem>1</iditem>
<quantity>1</quantity>
<price>800</price>
</item>
<item>
<iddoc>1</iddoc>
<iditem>2</iditem>
<quantity>1</quantity>
<price>200</price>
</item>
</items>
<taxes>
<tax>
<iddoc>1</iddoc>
<iditem>1</iditem>
<idtax>1000</idtax>
<value>123.90</value>
<tax>
<tax>
<iddoc>1</iddoc>
<iditem>2</iditem>
<idtax>1000</idtax>
<value>34.13</value>
<tax>
</taxes>
</rootxml>
to this one:
<resultxml>
<documento>
<iddoc>1</iddoc>
<total_price>1000</total_price>
<items>
<item>
<iddoc>1</iddoc>
<iditem>1</iditem>
<quantity>1</quantity>
<price>800</price>
<taxes>
<idtax>1000</idtax>
<value>123.90</value>
</taxes>
</item>
<item>
<iddoc>1</iddoc>
<iditem>2</iditem>
<quantity>1</quantity>
<price>200</price>
<taxes>
<tax>
<idtax>1000</idtax>
<value>34.13</value>
<tax>
</taxes>
</item>
</items>
</documento>
</resultxml>
I can't find out how to solve this. Although the original xml document can have several documents it will always have one, so I have to merge the items into it and taxes into its respective item.
Thank you
I think you'll have to write a chunk of custom-XSLT. Loop through items/item and then build an xpath to taxes/tax[where iditem=$itemId]. The trick is to get the first itemId into the variable.
Sorry, I have limited time right now, cannot create a full working demo for you.
Here's the related issue I had a while back:
http://www.stylusstudio.com/ssdn/default.asp?action=9&fid=48&read=7896
Neal Walters

Resources