With linq XML is it possible to add a new father to existing nodes?
Take this XML excerpt:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<items>
<book>
<title>Title 1</title>
<author>Author 1</author>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
</book>
<car>
<model>Tesla</model>
</car>
</items>
Is it possible to add a new father "books" to book like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<items>
<books>
<book>
<title>Title 1</title>
<author>Author 1</author>
</book>
<book>
<title>Title 2</title>
<author>Author 2</author>
</book>
</books>
<car>
<model>Tesla</model>
</car>
</items>
This is not working because it is cloning the nodes:
doc.Element("items").Add(new XElement("books",doc.Element("items").Elements("book")));
You can remove your existed <book> elements from <items> node after adding them under new <books> parent node:
var books = doc.Element("items").Elements("book");
doc.Element("items").Add(new XElement("books", books));
books.Remove();
Related
I wish to remove the Parent node and xml declaration from the file. Below is the input file.
<?xml version="1.0" encoding="utf-8"?>
<Items>
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
</Items>
I need to remove the XML declaration and parent node . Like below.
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
How could I do it using C#.NET?
You simply parse the XML with XDocument, like this:
var xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<Items>
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
</Items>";
var doc = XDocument.Parse(xml);
var s = doc.Root.Element("Product").ToString();
Console.WriteLine(s);
Which outputs
<Product>
<ID>001</ID>
<Name>John</Name>
<Designation>Developer</Designation>
</Product>
Remove namespaces, attributes, Xsi from Soap response using code or XSLT
Want to transform a soap response to a normal XML(without namespaces, atributes) using C# code (Serializer, XMLDoc, XDoc ) or XSLT.
here is the soap response.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:Magento"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:catalogProductInfoResponse>
<info xsi:type="ns1:catalogProductReturnEntity">
<product_id xsi:type="xsd:string">3459</product_id>
<sku xsi:type="xsd:string">HK-BP001</sku>
<categories SOAP-ENC:arrayType="xsd:string[0]" xsi:type="ns1:ArrayOfString"/>
<websites SOAP-ENC:arrayType="xsd:string[7]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</websites>
<created_at xsi:type="xsd:string">2016-04-19 01:45:35</created_at>
<has_options xsi:type="xsd:string">1</has_options>
<special_from_date xsi:type="xsd:string">2016-04-19 00:00:00</special_from_date>
<tier_price SOAP-ENC:arrayType="ns1:catalogProductTierPriceEntity[0]" xsi:type="ns1:catalogProductTierPriceEntityArray"/>
<custom_design xsi:type="xsd:string">ultimo/default</custom_design>
<enable_googlecheckout xsi:type="xsd:string">1</enable_googlecheckout>
</info>
</ns1:catalogProductInfoResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
i want transformed xml like :
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<catalogProductInfoResponse>
<info>
<product_id>3459</product_id>
<sku>HK-BP001</sku>
<categories/>
<websites>
<item>1</item>
</websites>
<created_at>2016-04-19 01:45:35</created_at>
<has_options>1</has_options>
<special_from_date>2016-04-19 00:00:00</special_from_date>
<tier_price/>
<custom_design>ultimo/default</custom_design>
<enable_googlecheckout>1</enable_googlecheckout>
</info>
</catalogProductInfoResponse>
</Body>
</Envelope>
You can use XSLT:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
I am trying to split a XML file to multiple small xml files in C#.net and am
trying to get the best possible approach to this. Any help on this will be
great... Sample example on what I am trying to do...
Source XML document
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>8510188</DOCID>
<ISSUE>2010</ISSUE>
<CAT>Literature and Art</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
<DOC>
<DOCID>1510179</DOCID>
<ISSUE>2012</ISSUE>
<CAT>Miscellaneous</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
Should split to two xml documents as below
1)
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>8510188</DOCID>
<ISSUE>2010</ISSUE>
<CAT>Literature and Art</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
2)
<?xml version="1.0" standalone="yes"?>
<DATABASE>
<DOC>
<DOCID>1510179</DOCID>
<ISSUE>2012</ISSUE>
<CAT>Miscellaneous</CAT>
<TITLE>Test</TITLE>
<TEXT>Test</TEXT>
</DOC>
</DATABASE>
Well, I'd use LINQ to XML:
XDocument doc = XDocument.Load("test.xml");
var newDocs = doc.Descendants("DOC")
.Select(d => new XDocument(new XElement("DATABASE", d)));
foreach (var newDoc in newDocs)
{
newDoc.Save(/* work out filename here */);
}
(I'm assuming you want to save them. Maybe you don't need to. I've tested this just by printing them out to the console instead.)
I have this XML:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book id="ISBN-NUMBER">
<title>
The Autobiography of Benjamin Franklin
</title>
<author type="major">
<first-name>
Benjamin
</first-name>
<last-name>
Franklin
</last-name>
</author>
<price>
8.99
</price>
</book>
<book id="ISBN-NUMBER">
<title>
The Confidence Man
</title>
<author type="major">
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
11.99
</price>
</book>
<book id="ISBN-NUMBER">
<title>
The Gorgias
</title>
<author type="major">
<name>
Plato
</name>
</author>
<price>
9.99
</price>
</book>
</bookstore>
How do i read it with XPath? I've used:
XPathDocument doc = new XPathDocument(stream);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator node = nav.Select("bookstore/book");
while (node.MoveNext())
but how do i move on from here? i guess i need a swtich statement to swtich if node is title, author, and price. if book then i need to read books id, same with author and type.
You can use node.Select('//title') to get the title, I suppose. I'm not sure how this works in C#, but common xpaths are as follows
//bokstore/book selects book nodes.
//bookstore/book/title selects the title node
//bookstore/book[n]/* selects all the all the child nodes of nth node.
How can i remove preprocessing instructions from an XML in a greener way? suppose that I have this xml in a string variable (which is a property of a class),I wanted to write it as value of another xml node,How can i achieve it in a cleaner way?
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
You help would be much appreciated !!
If you can load it in an XmlDocument: FirstChild returns the <?xml version="1.0" encoding="utf-8" ?> node, and NextSibling returns the rest.
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc.FirstChild.NextSibling;
Edit: Your xml looks a lot like the example on msdn for XPathNavigator.Select. Have you tried using that?
Edit2: You can get the name of the top level element using:
string topLevelNode = doc.DocumentElement.Name;
Try the code below. It will load the text to XmlDocument object first
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlAsText);
Then you can get the OutherXml using below:
xmlDoc.DocumentElement.OuterXml;