I have this XML:
<?xml version="1.0" encoding="UTF-8"?>
<LayerDefinition version="1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LayerDefinition-1.0.0.xsd">
<VectorLayerDefinition>
<ResourceId>ddddd</ResourceId>
<FeatureName>SHP_Schema:HydrographicPolygons</FeatureName>
<FeatureNameType>FeatureClass</FeatureNameType>
<Geometry>SHPGEOM</Geometry>
<VectorScaleRange>
<AreaTypeStyle>
<AreaRule>
<LegendLabel/>
<AreaSymbolization2D>
<Fill>
<FillPattern>Solid</FillPattern>
<ForegroundColor>FFABC7E9</ForegroundColor>
<BackgroundColor>FF000000</BackgroundColor>
</Fill>
<Stroke>
<LineStyle>Solid</LineStyle>
<Thickness>0</Thickness>
<Color>FFABC7E9</Color>
<Unit>Inches</Unit>
</Stroke>
</AreaSymbolization2D>
</AreaRule>
</AreaTypeStyle>
</VectorScaleRange>
</VectorLayerDefinition>
</LayerDefinition>
I need to change this element:
<BackgroundColor>FF000000</BackgroundColor>
To this:
<BackgroundColor>FFFFAAAA</BackgroundColor>
Here is the way I try to do it:
XmlDocument doc = new XmlDocument();
doc.LoadXml(layoutXml);
XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/BackgroundColor");
objNodeList.InnerXml = "FFFFAAAA";
But the code above dosent works.What I do wrong here why the attitude not works?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication75
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetDefaultNamespace();
XElement backgrounColor = doc.Descendants(ns + "BackgroundColor").FirstOrDefault();
backgrounColor.SetValue("FFFFAAAA");
}
}
}
I don't think you are going far enough down in your nodes. I think you need something more like this.
XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
objNodeList.selectSingleNode("BackgroundColor").innerXml= "FFFFAAAA";
Other wise your are trying to get a node inside VectorScaleRange that does not exist. Also you need the selectSingleNode() fucntion to grab the BackGroundColor node out of the list of nodes inside Fill.
Edited with the answer to your problem
XmlDocument doc = new XmlDocument();
doc.Load("texto.xml");
XmlNodeList objNodeList = doc.SelectNodes("/LayerDefinition/VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
objNodeList.Item(0).SelectSingleNode("BackgroundColor").InnerXml = "FFFFAAAA";
doc.Save("texto.xml");
Related
string content = ....
I have some XML content stored in a string as shown in the variable above. The content stored is similar to below ,
<?xml version="1.0"?>
<model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<ItemModel xsi:type="TypeKeep">
<Name>Name01</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name02</Name>
</ItemModel>
<ItemModel xsi:type="TypeDelete">
<Name>Name03</Name>
</ItemModel>
</Items>
</model>
In here I want to remove all the elements which have the type="TypeDelete" . In the sense I am trying to alter the content variable by removing the elments of TypeDelete
Any idea how I can achieve this ?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XNamespace xsiNs = doc.Root.GetNamespaceOfPrefix("xsi");
List<XElement> removeNodes = doc.Descendants("ItemModel").Where(x => (string)x.Attribute(xsiNs + "type") == "TypeDelete").ToList();
for (int i = removeNodes.Count - 1; i >= 0; i--)
{
removeNodes[i].Remove();
}
}
}
}
If you want to remove a specific XML node from an XML file directly you can go through this link and try this
I have a XML file which looks like:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<UFCC xmlns="http://actel.com/sweng/afi">
<Region name="Region_0_0">
<content>
<static_data>
<fixed>
<value>11111111111111111111111111111111</value>
<type>HEX</type>
</fixed>
</static_data>
</content>
<region_start_word>0</region_start_word>
<number_of_word>16</number_of_word>
<simulation_value>
<value>11111111111111111111111111111111</value>
<type>HEX</type>
</simulation_value>
</Region>
<Region name="Region_1_0">
<content>
<static_data>
<fixed>
<value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
<type>HEX</type>
</fixed>
</static_data>
</content>
<region_start_word>16</region_start_word>
<number_of_word>16</number_of_word>
<simulation_value>
<value>BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</value>
<type>HEX</type>
</simulation_value>
</Region>
<Program_Pages/>
</UFCC>
I'm trying to change value fields "11111111111111111111111111111111" to "A...A". I have tried this with Xml.Linq and using examples found online, from which none seem to work.
How can I modify these fields?
Sorry about the formatting, I'm not that familiar with proper XML formatting and the source file is written in single line.
You need to use the namespace :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
List<XElement> values1s = doc.Descendants(ns + "value").Where(x => (string)x == "11111111111111111111111111111111").ToList();
foreach (XElement x in values1s)
{
x.SetValue(((string)x).Replace("1", "A"));
}
}
}
}
Lets say I've a doctype :
<!DOCTYPE beer PUBLIC "-//BEER//DTD beer DTD version 2.0//KF//XML" "Kingfisher.dtd">
My requirement is I have a path(text) which needs to be appended to the doctype like this :
path = "C:/Beer/"
So after appending path to dtd reference, my doctype should look like this :
<!DOCTYPE beer PUBLIC "-//BEER//DTD beer DTD version 2.0//KF//XML" "C:/Beer/Kingfisher.dtd">
You certainly could do this with regex, but you shouldn't. There are much better xml manipulation tools in .net.
var xml = "<!DOCTYPE beer PUBLIC \"-//BEER//DTD beer DTD version 2.0//KF//XML\" \"Kingfisher.dtd\"><root></root>";
var xd = XDocument.Parse(xml);
var path = "C:/Beer/";
xd.DocumentType.SystemId = Path.Combine(path, xd.DocumentType.SystemId)
Using Xml Linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!DOCTYPE beer PUBLIC \"-//BEER//DTD beer DTD version 2.0//KF//XML\" \"Kingfisher.dtd\"><root></root>";
XDocument doc = XDocument.Parse(xml);
XDocumentType _type = (XDocumentType)doc.Nodes().Where(x => x.NodeType == XmlNodeType.DocumentType).FirstOrDefault();
_type.SystemId = "C:/Beer/Kingfisher.dtd";
}
}
}
Assuming I have the following XML files:
Sample1.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<NodeA>
<Node1>test1</Node1>
<Node1>test2</Node1>
</NodeA>
</Root>
Sample2.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<NodeB>
<Node1>test1</Node1>
<Node1>test2</Node1>
</NodeB>
</Root>
My goal is to deserialize those 2 xml files using one factory method.
For that I have the following generic code:
public static T get<T>() where T : class // class = all reference types
{
string Ttype = typeof(T).Name;
using (var streamReader1 = new StreamReader(directoryPath + #"\Sample1.xml"))
using (var streamReader2 = new StreamReader(directoryPath + #"\Sample2.xml"))
using (var xmlReader ??? = XmlReader.Create(streamReader))
{
...
}
}
Built both xml files with the same root element.
My question is, since the xmlReader create function accepts only 1 stream reader.
How in the name of *, do i read with the xml reader from 2 xmls?
Do I combine 2 streams into 1 and read from 1st bit to the last 1, if so, how?
Thanks in advance,
Montana.
I would do it like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME1 = #"c:\temp\test.xml";
const string FILENAME2 = #"c:\temp\test2.xml";
static void Main(string[] args)
{
XDocument doc1 = XDocument.Load(FILENAME1);
XElement root1 = (XElement)doc1.FirstNode;
XDocument doc2 = XDocument.Load(FILENAME2);
XElement root2 = (XElement)doc2.FirstNode;
root1.Add(root2.Elements());
}
}
}
My issue is, when selecting a node from xml file (packet in my case) is decoding an url that is the inner text of the node within the xml. Example
<url>"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2"</url>
using an instance of XmlDocument:
xmlDoc.SelectSingleNode("url").InnerText;
The string that is returned is: "http://my.xml.org/?id=AAA/DDDD/NNNLKLKJLKL/"
How do I prevent this from happening? Notice that the %2 and %3's have been changed to forward slashes.
The code below gets correct results. What are you doing differently?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<url>\"http://my.xml.org/?id=AAA%2DDDD%3dNNNLKLKJLKL%2\"</url>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string results = xmlDoc.SelectSingleNode("url").InnerText;
}
}
}