Convert node section into one line - c#

i have xml document with a lot of items like this:
<item>
<key>
<unsignedShort>x</unsignedShort>
</key>
<value>
<unsignedShort>y</unsignedShort>
</value>
</item>
<item>
......
now i want that every section of it will look like this:
<item><key><unsignedShort>x</unsignedShort></key><value><unsignedShort>y</unsignedShort></value></item>

You would have to implement your own XmlWriter. XmlTextWriter allows you to use an unindented format, but that would result in the entire document being on one line. So, yeah, you'd have to roll your own, unless you're OK with the entire document sitting on one line.

Related

How to update all instances of an element in XDocument?

I have xml structure as below. How can I replace the element value of that is present everywhere in this structure? Is there a way to do this using Linq? Also, sometimes the structure could be different but there will always be Resource element so I need to look at all instances of Resource and not care about where it is present.
Thanks for any suggestions.
<Users>
<User>
<Number>123456</Number>
<ID>1</ID>
<Events>
<Event>
<ID>12</ID>
</Event>
</Events>
<Items>
<Item>
<ID>12</ID>
<Resource>Replace this value</Resource>
</Item>
<Item>
<ID>13</ID>
<Resource>Replace this value</Resource>
</Item>
<Item>
<ID>14</ID>
<Resource>Replace this value</Resource>
</Item>
</Items>
</User>
//More User elements where Resource needs to be updated
<User>
</User>
<User>
</User>
</Users>
Linq is a query language, so you can't directly use it to modify the value, but you can easily select all the Resource elements in the document with it and iterate/change them.
For example:
// or load from xml, however you have it
var xDoc = XDocument.Load(#"c:\temp\myxml.xml");
// iterate every Resource element
foreach (XElement element in xDoc.Descendants("Resource"))
element.Value = "Hello, world";
That will pick out every Resource element in the XML regardless of where it is in the hierarchy, which in your case, is what you need. If you needed to target it more specifically, you could either use an XPath expression or further Linq calls such as Element() which work on a single level of the hierarchy.

Identifying a single XML element and calling in C#

I currently have a program(C#) I am working on that uses an XML file to keep and item and its sell and buy price. But I need to figure out how to identify and call just a single section such as the "pristine Robot Brainstorm Bulb" in my C# project.
I need to be able to call a separate section rather than the whole lot of it.
Here is my XML
<items>
<Item>
<itemName>Pristine Robot Brainstorm Bulb</itemName>
<defindex>5701</defindex>
<maxAmount>25</maxAmount>
<sellPrice>4</sellPrice>
<buyPrice>0</buyPrice>
</Item>
<Item>
<itemName>Pristine Robot Currency Digester</itemName>
<defindex>5700</defindex>
<maxAmount>25</maxAmount>
<sellPrice>4</sellPrice>
<buyPrice>0</buyPrice>
</Item>
<Item>
<itemName>reinforced robot emotion detector</itemName>
<defindex>5702</defindex>
<maxAmount>150</maxAmount>
<sellPrice>.5</sellPrice>
<buyPrice>0</buyPrice>
</Item>
<Item>
<itemName>reinforced robot humor suppression pump</itemName>
<defindex>5703</defindex>
<maxAmount>150</maxAmount>
<sellPrice>.5</sellPrice>
<buyPrice>0</buyPrice>
</Item>
Just use Linq to Xml to query for the specific node that you are looking for, based the any of the values of the child elements.
var xDoc = XDocument.Parse("<my xml>");
string itemName = "Pristine Robot Brainstorm Bulb";
var item = xDoc.Root.Elements("Item")
.FirstOrDefault(x=>x.Element("itemName").Value == itemName);
If your aim is to call them from .cs file then simply give name to your elements.
<Item x:Name = "P_R_C_Register">
<itemName>Pristine Robot Currency Digester</itemName> //and so on...
Then intellisense will show you your element when you are coding C#. I hope I understood your problem correct.

Edit Xml InnerText accessed as Plain Text using RegEx?

I have the following dummy fake sample:
<family>
<member> dad </member>
<member> mum </member>
<member> son </member>
<member> grandad<> </member>
</family>
I have been given a document to convert into XML but I have been unsuccessful so far in doing so. I have no control over how the document (html) given to me is created but I need to convert the document to xml; So that I can convert it using a stylesheet.
TidyManaged and HAP are no good to me at this stage in my workflow. Will explain more if people are interested knowing why.
In order for me to use HAP successfully, I need the above sample to look like the below:
<family>
<member> dad </member>
<member> mum </member>
<member> son </member>
<member> grandad<> </member>
</family>
My last approach before I give up on this problem would be, to read in my source html document, treat it as a plan text document and read it line by line.
I require someone to give me some regex that will successfully match the inner text of an element i.e:
<member> grandad<> </member>
Would give me the string:
"grandad<>"
If I can get this far, I should be able to convert the angle brackets into html key code equivalents. This should then pass as valid XML allowing me to load this into an XDocument class.
Then replace that result string back with this one:
<member> grandad<> </member>
When all special characters have been 'escaped' like this properly then I will be in a position to leverage the benefits of HTML Agility Pack (HAP) otherwise I will have to give up.
Thanks for reading.
The simplest Regular Expressions
var reg = new Regex(#"(?<=<(\w+)>)(.*)(?=</\1>)");
var input = "<member> grandad<Regexp is a bad tool because of <strong>this</strong>> </member>";
var output = reg.Match(input).Value;
Problem will be if your member tag contains any white spaces or attributes or more then one member tag will be in single line. So if you can provide ugliest example I'll change expression to adjust your input.
If you can process each document manually then you can use notepad++.
The reindent xml(TextFX->TextFX HTML Tools->Reindent xml> functionality will automatically impose the entities you want.

How can I use XMLReader to read number of elements below specific node element?

Lets say you have the following xml convention which can't be replaced:
Each class is represented By <Class> and HashCode attribute.
The name of the class is right below the <Class> tag.
<?xml version="1.0" encoding="utf-8"?>
<Class HashCode="1">
<Person>
<Class HashCode="-3">
<FullName>
<FirstName>Dan</FirstName>
<LastName>K</LastName>
</FullName>
</Class>
<Age>20</Age>
<Class HashCode="4">
<Address>
<Street>abc</Street>
<City>new york</City>
<ZipCode>30500</ZipCode>
<PhoneNumber>1245</PhoneNumber>
</Address>
</Class>
</Person>
Question: How can I use XMLReader object in order to read the number of Node elements after a given element, until I reach an element tag which wasn't followed by <Class> tag.
For example:
Starting at <FullName> will automatically stop at <LastName> (because one tag before there is no <Class> tag) and return the number of elements between them which I guess is 1.
Assumption: Consider you have the start tag's Hashcode value and Name which will come of course one level after <Class Hashcode=> tag.
I tried many things with XMLReader methods and got complicated, was trying to convert to XMLDocument with XMlReader.ReadSubtree() but seems that XMLDocument didn't help me also.

What's the quickest (code execution) way to execute an XML reading?

I have to read the XML:
<items>
<item>
<prop1>value1</prop1>
<prop2>value2</prop2>
<prop3>value3</prop3>
</item>
<item>
<prop1>value1</prop1>
<prop2>value2</prop2>
<prop3>value3</prop3>
</item>
</items>
And put the values into a List<CLASS>.
Some options:
Use XMLSerializer to deserialize to a List
Use XMLDocument to read each item using SelectNodes with XPath and put the values into a List
Use XMLReader to read each node and put the values into a List
Other option...
By far the fastest that I have seen is to use XSD.exe to create an XSD and Class to go with it, then use serialization.
Another option would be to use LinqToXml.
If you're in dotnet, install the WCF starter pack. Then you'll have an option "Paste XML as Types", so you can cut the XML you're looking to serialize into the clipboard and paste it into code as a serializable type. Then you can just serialize the XML and get the values through the class.

Categories

Resources