I have the following xml:
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB CCC="it is aatr 1" DDD="it is attr 2">
</AAA>
I want in my c# application to add several BBB nodes than my xml will look like this:
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB CCC="it is aatr 1" DDD="it is attr 2">
<BBB CCC="it is another attr 1" DDD="it is another attr 2">
<BBB CCC="this is something else 1" DDD="this is something else 1">
</AAA>
any suggestion of code?
You may use DOM (System.Xml) and Linq to XML api.
Linq to XML
XDocument doc = XDocument.Load(file);
XElement ele = new XElement("BBB");
ele.SetAttributeValue("CCC", "text1");
ele.SetAttributeValue("DDD", "text2");
doc.Root.Add(ele);
doc.Save(file);
Related
I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ?
For removing a node in main xml I am doing
var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002");
if (result != null)
{
result.Remove();
}
Here is my XML :
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1>id="001" version="1.0"</node1>
<node2>id="002" version="1.0"</node1>
<report>raw = "<response = "10"><innerxml><prod>date = "18082016" name="pqr"</prod><seg1>id="002" name = "sqs"</seg1></innerxml></response>"</report>
</root>
Your code is correct but your xml is not. the XML should be like:
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1 id="001" version="1.0"></node1>
<node2 id="002" version="1.0"></node2>
</root>
I have two XML documents.
My objective is to replace one of the nodes in the first document with the entire contents of the second Xml documents.
So the first document - Parent looks something like this:
<Root>
<AgencyName = "Some Agency"/>
<Originator = "Some other Agency"/>
<Type = "AnonymousType"/>
<Details/>
</Root>
The second document - children looks like this:
<Root>
<Details>
<Detail1>
...
</Detail1>
<Detail2>
...
</Detail2>
<Detail3>
...
</Detail3>
</Details>
</Root>
The node <Details/> has to be replaced with the contents of the second document.
I am attempting to use Linq to XML to do this. The first document is represented in an XDocument class and the second one is represented in an XElement class. There are several child attributes for <Detail/> and I haven't listed them here.
I am attempting to replace the element from the first document with this XElement class.
If I try something like this,
ParentDoc.Element("Details").ReplaceAll(children);
it is unlikely to work. How should I do the replace?
var doc = XDocument.Load(#"C:\Tools\test.xml");
var doc2 = XDocument.Load(#"C:\Tools\test2.xml");
var children = doc2.Root.Element("Details");
var parentNode = doc.Root.Element("Details");
parentNode.ReplaceWith(children);
By the way, your xml are not correct, so you'll get exceptions.
I tried with
<Root>
<AgencyName name= "Some Agency"/>
<Originator name= "Some other Agency"/>
<Type name= "AnonymousType"/>
<Details/>
</Root>
and
<Root>
<Details>
<Detail1>
asdf
</Detail1>
<Detail2>
asde
</Detail2>
<Detail3>
eere
</Detail3>
</Details>
</Root>
and got
<?xml version="1.0" encoding="utf-8"?>
<Root>
<AgencyName name="Some Agency" />
<Originator name="Some other Agency" />
<Type name="AnonymousType" />
<Details>
<Detail1>
asdf
</Detail1>
<Detail2>
asde
</Detail2>
<Detail3>
eere
</Detail3>
</Details>
</Root>
I have an xml file like this,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<ContentSelector datavalue_idtype="content:smartform" datavalue_displayvalue="«Smart Form:49»">49</ContentSelector>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
I want to modify it like the one below,
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Title>HomePage</Title>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<Products>
<Product>
<ProductTitle>Product</ProductTitle>
<ProductDate>03-08-2012 11:57:25</ProductDate>
<ProductImage>
<img src="ex1.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex2.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
<ProductImage>
<img src="ex3.jpg" />
</ProductImage>
<ProductThumbnailImage>
<img src="ex4.jpg" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
</Product>
</Products>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>
All the new nodes and values are to be added through a C# function. Let us assume these new values as static values for now.
Also the node "FeaturedProduct" is not only one. There are a lot of nodes in that name. I want to modify all the "FeaturedProduct" nodes.
You should take a look at the XDocument class here:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
I had to do something very similar to this a couple of weeks ago; XDocument is more willing to manipulate data, and using LINQ with it is incredibly easy.
This doesn't use linq exactly, but It will serve your purpose
XmlDocument xDoc = new XmlDocument();
xDoc.Load("filename.xml");
foreach (XmlNode xNode in xDoc.SelectNodes("//FeaturedProduct"))
{
XmlElement newElement = xDoc.CreateElement("newElementName");
XmlAttribute newAttribute = xDoc.CreateAttribute("AttributeName");
newAttribute.Value = "attributeValue";
newElement.Attributes.Append(newAttribute);
xNode.AppendChild(newElement);
xNode.InnerText = "myInnerText";
}
Also, This Documentation is a very handy reference for Xpath
Here's how to do it with Linq:
string documentXml = #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<root>
<FeaturedProductCategories>
<FeaturedProductCategory>
<FeaturedProducts>
<FeaturedProduct>
<ContentSelector datavalue_idtype=""content:smartform"" datavalue_displayvalue=""«Smart Form:49»"">49</ContentSelector>
</FeaturedProduct>
</FeaturedProducts>
</FeaturedProductCategory>
</FeaturedProductCategories>
</root>";
string productsXml = #"<Products>
<Product>
<ProductTitle>Product</ProductTitle>
<ProductDate>03-08-2012 11:57:25</ProductDate>
<ProductImage>
<img src=""ex1.jpg"" />
</ProductImage>
<ProductThumbnailImage>
<img src=""ex2.jpg"" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
<ProductImage>
<img src=""ex3.jpg"" />
</ProductImage>
<ProductThumbnailImage>
<img src=""ex4.jpg"" />
</ProductThumbnailImage>
<ProductCaption>Product Caption</ProductCaption>
</Product>
</Products>";
XDocument document = XDocument.Parse(documentXml);
var targetNodes = from featuredProduct in document.Descendants("FeaturedProduct")
from contentSelector in featuredProduct.Elements("ContentSelector")
select contentSelector;
foreach (var targetNode in targetNodes)
{
targetNode.ReplaceWith(XElement.Parse(productsXml));
}
Console.WriteLine(document.ToString());
i have 2 dropdownlist to implement and the xml file is as follow:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder id="1" value="builder#xyz.com">
</builder>
<builder id="2" value="Others">
</builder>
</builderemail>
<manageremail>
<manager id="1" value="manager#xyz.com"></manager>
<manager id="2" value="Others"></manager>
</manageremail>
</email>
i am using visual studio 2005 and i tried to data bind each drop down list by choosing a data source. my requirements are that one of the drop downlist for builder's email and the other drop down list for manager's email.
How do i do that by using the datasource configuration wizard?
regards
These links might help you out
http://www.codeproject.com/KB/aspnet/xmlboundlistcontrol.aspx
http://dotnetspidor.blogspot.com/2009/04/bind-aspnet-dropdownlist-to-xml-file.html
But you might have to change your xml and structure it like
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>builder#xyz.com</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>manager#xyz.com</value>
</builder>
</manageremail>
</email>
Hope this helps
You need to parse xml and store the builderemail and manageremail in list, then provide as datasource to dropdown list
XmlDocument doc = new XmlDocument();
doc.Load(#"sample.xml");
XmlNodeList builderNodes = doc.GetElementsByTagName("builderemail");
XmlNodeList mangerNodes = doc.GetElementsByTagName("manageremail");
List<string> builderMails = new List<string>();
foreach (XmlNode node in builderNodes[0].ChildNodes)
{
builderMails.Add(node.Attributes["value"].Value);
}
List<string> mangerMails = new List<string>();
foreach (XmlNode node in mangerNodes[0].ChildNodes)
{
mangerMails.Add(node.Attributes["value"].Value);
}
comboBox1.DataSource = builderMails;
comboBox2.DataSource = mangerMails;
I have an xml document that has a structure like so:
<?xml version="1.0" encoding="iso-8859-1" ?>
- <newdataset xml="version="1.0" encoding="iso-8859-1"">
- <officelist>
<officeid>2</officeid>
<office>Office</office>
<region>BC</region>
I would like to have the office id = 2 to be its own element. Like so
<?xml version="1.0" encoding="iso-8859-1" ?>
<newdataset xml="version="1.0" encoding="iso-8859-1"">
<officelist>
<officeid id=2/>
<office>Office</office>
<region>BC</region>
</officeid>
</officelist>
</newdataset>
xmlDS += offices.GetXml();
xmlDS = xmlDS.Replace(#"xml:space=""preserve""", " ");
XmlDocument doc = new XmlDocument();
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.LoadXml(xmlDS);
doc.Save(Response.OutputStream);
That is my code so far... not sure how to set a child node to become a parent node
using XDocument made this a lot easier with Linq.