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.
Related
I need to change ALL the Tag name to Lowercase but leave the InnerText or Value as it is. I just found the thread showing how to change the casing for the entire document but NOT just for the Tag names.
Code
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDoc.OuterXml.ToLower());
Original.xml
<?xml version="1.0" encoding="utf-8" ?>
<ROOT>
<InNeRtAg>SoMe TeXt</InNeRtAg>
<NeStEdElEmEnT>
<InNeRnEsTiNg>SoMe TeXt</InNeRnEsTiNg>
</NeStEdElEmEnT>
</ROOT>
Changing ALL nodes including the root element:
var doc = XDocument.Load("Original.xml");
// also need to change the root element
doc.Root.Name = doc.Root.Name.LocalName.ToLower();
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}
doc.Save("Modified.xml");
Results in Modified.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<innertag>SoMe TeXt</innertag>
<nestedelement>
<innernesting>SoMe TeXt</innernesting>
</nestedelement>
</root>
When using
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}
the root element will not be changed.
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<innertag>SoMe TeXt</innertag>
<nestedelement>
<innernesting>SoMe TeXt</innernesting>
</nestedelement>
</ROOT>
You can try something like this:
var doc = XDocument.Load(filepath);
foreach (var element in doc.Descendants().Elements())
{
element.Name = element.Name.LocalName.ToLower();
}
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 am trying to make an XML document through C# with a specific format, but I am having trouble getting it to look right. Here is what it comes out as:
<?xml version="1.0" encoding="UTF-8" ?>
<loanRequest d1p1:ssn=""
d1p2:creditScore=""
d1p3:loanAmount=""
d1p4:loanDuration=""
xmlns:d1p4="26-08-2015 12:41:11"
xmlns:d1p3="147862"
xmlns:d1p2="266"
xmlns:d1p1="765383-2478" />
Here is what it should have been:
<?xml version="1.0" encoding="UTF-8" ?>
<LoanRequest>
<ssn>765383-2478</ssn>
<creditScore>266</creditScore>
<loanAmount>147862</loanAmount>
<loanDuration>2015-08-26 12:41:11.0 CET</loanDuration>
</LoanRequest>
XML manipulation in C# is really confusing to me and I have a hard time piecing together how to do it. The code that I use to make the XML document is as follows:
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode loanRequest = doc.CreateElement("loanRequest");
XmlAttribute ssn = doc.CreateAttribute("ssn", l.SSN);
XmlAttribute creditscore = doc.CreateAttribute("creditScore", ""+l.CreditScore);
XmlAttribute loanamount = doc.CreateAttribute("loanAmount", ""+l.LoanAmount);
XmlAttribute loanduration = doc.CreateAttribute("loanDuration", l.LoanDuration.ToString());
loanRequest.Attributes.Append(ssn);
loanRequest.Attributes.Append(creditscore);
loanRequest.Attributes.Append(loanamount);
loanRequest.Attributes.Append(loanduration);
doc.AppendChild(loanRequest);
Why not use Linq to Xml
XDocument doc = new XDocument(
new XComment("this is a comment"),
new XElement("LoanRequest",
new XElement("ssn", l.SSN),
new XElement("creditScore", l.CreditScore),
new XElement("loanAmount", l.LoanAmount),
new XElement("loanDuration", l.loanDuration.ToString())));
doc.Save(path);
Apparently you add as attributes what should be nodes. Replace the calls of CreateAttribute with calls of AppendChild.
I want to read the following XML file:
Words.xml:
<?xml version="1.0" encoding="utf-8" ?>
<words>
<word>Bat</word>
<word>Dog</word>
<word>Car</word>
</words>
..using XDocument. I keep getting a "Non white space characters cannot be added to content" error with this code:
XDocument doc = new XDocument("words.xml");
foreach (XElement element in doc.Descendants("word"))
{
Console.WriteLine(element.Value);
}
You need to load the document like this:
XDocument doc = XDocument.Load("words.xml");
The reason your original code fails is you are using XDocument (Object[]) constructor that normally expects list of XElement objects like:
var doc = new XDocument(new XElement("Root"));
I am trying to find a node in my xml file but getting the error ( see title)?
// instantiate XmlDocument and load XML from file
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\temp\test2.xml");
var node = doc.SelectSingleNode("/Offers/Offer/ID=[text()='1']");
var test = node;
xml
<?xml version="1.0" encoding="utf-8"?>
<Offers>
<Offer>
<Model>AAAA</Model>
<ID>1</ID>
<Name>First offer</Name>
</Offer>
<Offer>
<Model>BBBB</Model>
<ID>2</ID>
<Name>Second offer</Name>
</Offer>
</Offers>
Remove the = after ID:
var node = doc.SelectSingleNode("/Offers/Offer/ID=[text()='1']");
becomes:
var node = doc.SelectSingleNode("/Offers/Offer/ID[text()='1']");