XmlDiff.Compare with XmlNodeList - xmlnodelist

I need to compare only specific nodes from two XML files.
Suppose if both the XML structure is as below:
XML 1:
<Species>
<Human>
<Number>2</Number>
</Human>
<Human>
<Number>3</Number>
</Human>
<Human>
<Number>10</Number>
</Human>
<Bird>
<Number>20</Number>
</Bird>
<Human>
<Number>8</Number>
</Human>
</Species>
XML 2:
<Species>
<Human>
<Number>2</Number>
</Human>
<Bird>
<Number>2</Number>
</Bird>
<Human>
<Number>8</Number>
</Human>
<Human>
<Number>3</Number>
</Human>
<Human>
<Number>10</Number>
</Human>
<Bird>
<Number>20</Number>
</Bird>
</Species>
I want to compare all the "Human" nodes, so I will declare an XMLNodeList and populate its values using XMLNode.SelectNode("//Species/Human")
But I can pass only XmlNode in XMLCompare. Also I need to apply IgnoreChildOrders for the XMLDiffPatch tool : I am using "Compare" method.
How do I pass a XmlNodeList to Compare method?

Related

How to suppress root element in BeanIO XML generation?

When generating a XML file with BeanIO, the StreamBuilder name is used as root element. How to suppress this root element?
Example:
StreamBuilder builder = new StreamBuilder("builder_name")
.format("xml")
.parser(new XmlParserBuilder()).addRecord(Test.class);
The Test class:
#Record
public class Test {
#Field(at=0)
private String field1 = "ABC";
// Getters and Setters ...
}
The generated XML file:
<builder_name>
<Test>
<field1>ABC</field1>
</Test>
</builder_name>
I don't want builder_name to be showed as root element. I want Test element to be the root. How can I achieve that?
You need to set the xmlType property/attribute to XmlType.NONE on your stream configuration.
The answer is in Appendix A - XML Mapping file reference of the documentation.
xmlType - The XML node type mapped to the stream. If not specified or set to element, the stream is mapped to the root element of the XML document being marshalled or unmarshalled. If set to none, the XML input stream will be fully read and mapped to a child group or record.
The trick is now to translate that piece of information into the StreamBuilder API, which is:
xmlType(XmlType.NONE)
Your example then becomes:
StreamBuilder builder = new StreamBuilder("builder_name")
.format("xml")
.xmlType(XmlType.NONE)
.parser(new XmlParserBuilder())
.addRecord(Test.class);
Which produces this unformatted/unindented output:
<?xml version="1.0" encoding="utf-8"?><test><field1>ABC</field1></test>
To have the xml formatted (pretty print)/indented use:
StreamBuilder builder = new StreamBuilder("builder_name")
.format("xml")
.xmlType(XmlType.NONE)
.parser(new XmlParserBuilder()
.indent()
)
.addRecord(Test.class);
Note the change to the XmlParserBuilder, to produce this output:
<?xml version="1.0" encoding="utf-8"?>
<test>
<field1>ABC</field1>
</test>

ReactJS dynamically call custom class

I am trying to setup a way to create form objects dynamically from some json values. Essentially, I have in my json the form object type and properties. I pass that type to a FormInput class that then calls the custom class containing the actual form object. My problem right now is that when I pass in the custom class name "TextInput" (this.props.formElementType) React just creates an element called 'textinput' instead of calling the class. It doesn't appear to like passing in a string, but wants just the classname. Essentially,...
TextInput = React.createClass({...})
...
FormItem = React.createElement(<TextInputPassedAsAString>, {...})
I am not sure if I can call a custom class this way or not, by passing a string. I need help with this implementation or a better idea as I am relatively new to React.
Below is all the relevant code starting with the children ending with the final render block. Please excuse the pseudo coffeescript.
TextInput
TextInput = React.createClass
handleChange: (event)->
this.setState
value: event.target.value
render: ->
React.createElement('label', {
value: this.props.formElementLabel
})
React.createElement('input', {
id: this.props.formElementID,
type: 'text'
})
module.exports = TextInput
FormElement
FormElement = React.createClass
render: ->
R.div(null,
React.createElement(this.props.formElementType, {
formElementID: this.props.formElementID,
formElementLabel: this.props.formElementLabel
})
module.exports = FormElement
The initial call/final render
React.createElement(FormElement, {
formElementType: 'TextInput',
formElementID: 'firstFormElement',
formElementLabel: 'First text input'
})
Well, the best way, easiest to reason about, etc. is probably to require TextInput in the module that's doing your final render. Create it there, passing in the other props to it, and then pass this created component to FormElement rather than passing the string representation of the component.
One way to do this more dynamically would be to have have a module that exports each dynamic component as a property/method on the export. Something like:
module.exports = {
TextInput: ...
}
Then when you're rendering you could pass in something like this:
myImports[json.formElementType]

Adding an element id with coffescript

I am writing a class in coffescript and am able to add a class name with the following:
class Str extends Spine.Controller
className: 'myClass'
module.exports = Str
This outputs a div with the class 'myClass'. But how do I assign an element id??? I'm basically trying to output the following:
<div id="myId" class="myClass"></div>

WPF Data binding C# in xaml

I would like to make the following code to be XAML:
CData cData = new CData();
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
mc1.Data = cData;
mc2.Data = cData;
How to make the above code to be XAML format?
Since the property .Data is a custom class CData and Both mc1 and mc3 has to point to the same cData. How to make it in XAML?
This should do:
<Window.Resources>
<local:CData x:Key="CData"/>
<local:MyClass x:Key="mc1" Data="{StaticResource CData}" />
<local:MyClass x:Key="mc2" Data="{StaticResource CData}" />
</Window.Resources>
where local points to your namespace where both classes are defined.

Control XML Serialization from DataSet -- Attributes on "TableName element"

Hope I chose the correct forum.
I have a dataset object with one table that comes to me from a common custom component's GetDS method. I need to pass XML to another process (chunked as byte array). I have it all working but the XML is missing some attributes that the consuming process expects.
I create a dataset object and can control the name of the TableName (root element) and the row like this:
da.Fill(ds, "Foo")
ds.DataSetName = "FooUpload"
I use the GetXML method to serialize to XML that looks like the following:
<?xml version="1.0" standalone="yes" ?>
<FooUpload>
<Foo>
<FooMasterID>483</FooMasterID>
<Country>27</Country>
<PaymentCode>ANN</PaymentCode>
<Amount>132</Amount>
<PaidDate>2012-12-31 00:00:00</PaidDate>
<PaidBy>FooServices</PaidBy>
</Foo>
</FooUpload>
The calling process expects
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<FooUpload **ClientCode="FOOO" RecordCount="1" CreateDate="2008-12-09T15:02:18.920" CreateUser="valli"**>
<Foo>
<FooMasterID>483</FooMasterID>
<Country>27</Country>
<PaymentCode>ANN</PaymentCode>
<Amount>132</Amount>
<PaidDate>2012-12-31 00:00:00</PaidDate>
<PaidBy>FooServices</PaidBy>
</Foo>
</FooUpload>
Note the attributes on the FooUpload element. This node is the name of the DataTable in the DataSet.
I have searched for how to control the XMLSerializer and find lots of examples for custom objects. I even found examples of setting the column mapping to be MappingType.Attribute which is close but I need to do this with the root element which is actually the TableName for the dataset.
I feel that I am close and if I do not find a more elegant solution I will have to create a hack like looping and spitting out changed string plus rest of the XML.
You can feed the output of GetXML into a XmlDocument and add the attributes afterwards. For example:
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(ds.GetXml());
XmlAttribute attr=xdoc.CreateAttribute("ClientCode");
attr.Value = "FOOOO";
xdoc.DocumentElement.Attributes.Append(attr);
Then you can save the xdoc into a file, or put it into a string, for example:
XmlTextWriter xw = new XmlTextWriter(new MemoryStream(),Encoding.UTF8);
xdoc.Save(xw);
xw.BaseStream.Position = 0;
StreamReader sr = new StreamReader(xw.BaseStream);
string result = sr.ReadToEnd();
This looks pretty much as what you are looking for.