ideas to modify attributes of different XML files c#
here is my scenario,
- I copy XML Files from the Original Directory to the Target Directory
- I modify the file's attributes in the Original Directory
- I compare the modified file's attribute(from the Original Directory) with the copied file's attribute (in the Target Directory) They shouldn't be the same of course
My problem is that NOT all XML Files have the same attributes.
I tried adding a common attribute and modify it but the files are retrieved from a DB and each attribute represents a coloumn there and I can't modify the DB
I'm thinking of looping through the attributes, till I reach the first "non-fixed" attribute and modify it. Is that possible? if so how? and if there is other solutions I'll appreciate it even more :)
Here is an example of part of my xmls
<CtApproachTypes
DataclassId="1992A9CE-B048-4676-BFD4-FD81F1A65401"
EntityId="1992A9CE-B048-4676-BFD4-FD81F1A65401"
Name="PAR"
Remark="No Remarks"/>
</CT_ApproachTypes>
<MiMissions
DataclassId="C196A66B-4FA1-461C-9EEF-95A4F2085051"
EntityId="C196A66B-4FA1-461C-9EEF-95A4F2085051"
MissionName="Standard"
isib="1"
</MiMissions>
<StSituations
DataclassId="679FAC3C-C9EF-41FD-9A13-957915605F01"
EntityId="679FAC3C-C9EF-41FD-9A13-957915605F01"
SitName="Standard"
Status="C"
Template="1">
</StSituations>
I wanna skip the first two attributes and modify the first attribute after them.
Note: I checked the XML Files and all of them seem to have an attribute with "-name" in them. I was thinking of using "like" to query.. but a more solid solution would be to modify the first attribute after the fi开发者_如何学Gorst 2.
Thanks
If what you're trying to do is update attributes of XML this could be one way:
XElement elem = XElement.Load("xmlFilePath");
System.Xml.XPath.Extensions.XPathSelectElement(elem, "xpath").SetAttributeValue("attributeName", "newValue");
where xmlFilePath is the path to the XML you're changing, xpath is the path to the element of the xml and attributeName is the name of the nodes attribute you're altering... Ofcourse, you have to add a custom node with attribute to every xml you're using.
EDIT:
Since you said you can't modify the XML files in both locations, than all you're left with is to compare the FileInfo
properties of the files themselves.
And if the FileInfo
properties are not what you need, then there's no other solution but to create a tracking db or a file, which will contain attribute values for your Original and Target directory, by which you can compare and do appropriate action.
For example, if your Original and Target maps 1 on 1, than you could have a simple table structure like this 'TableName, OriginalValue, TargetValue'. Where you'd keep this 'tracking' data (db, xml file, other...) depends on the number of entries, and your current architecture...
EDIT2: If you wan't to append to the first attribute found you can use this:
XElement elem = XElement.Load("myfilepath.xml");
elem.FirstAttribute.SetValue(elem.FirstAttribute.Value + "|myothervalue");
精彩评论