XMLString needs to change the format - c#

my xml DATA IS like:(This is xmlstring not an xmlfile and i need to transform without saving....)
<ProductGroups>
<ProductGroup>
<Name>ABC</Name>
<Id>123</Id>
</ProductGroup>
<ProductGroup>
<Name >xyz</Name>
<Id>456</Id>
</ProductGroup>
<ProductGroup>
<Name>PQR</Name>
<Id>789</Id>
</ProductGroup>
.
.
</ProductGroups>
I want to transform like this
<PRODUCTGROUPS>
<Name ID="123"> ABC</NAME>
<Name ID="456"> XYZ</NAME>
<Name ID="789">PQR</NAME>
.
.
</PRODUCTGROUPS>
I'm using C# with .NET.

From memory, may contain some errors:
var doc = XDocument.Load(...);
var groups = doc.Descendants(ProductGroup);
var newDoc = new XElement("ProductGroups",
groups.Select(pg => new XElement("Name",
new XAttribute("Id", pg.Element("Id").Value),
pg.Element("Name").Value) ));
newDoc.Save(...);

You could try it with linq2xml : (EDIT : roughly the same aproach as Henk Holterman)
XDocument sourceDocument = XDocument.Load("D:\\XmlFile.xml");
XDocument targetDocument = new XDocument();
var productGroupsElement = new XElement("ProductGroups");
sourceDocument.Descendants("ProductGroup").ToList().ForEach(productGroup =>
{
if (productGroup.Element("Name") != null && productGroup.Element("Id") != null)
{
var nameElement = new XElement("Name", productGroup.Element("Name").Value);
nameElement.Add(new XAttribute("Id", productGroup.Element("Id")));
productGroupsElement.Add(nameElement);
}
});
targetDocument.Add(productGroupsElement);
var resultXml = targetDocument.ToString(SaveOptions.None);

Related

Merge two XML files in C# where the tags ID is the same

I need to merge two XML files in C# where AccountNumber is the same in these XMLfiles.
I have about 400 accounts in the XML file.
XML 1:
<?xml version="1.0" standalone="yes"?>
<Account>
<Table1>
<Id>12</Id>
<AccountNumber>5050</AccountNumber>
<External>false</External>
</Table1>
</Account>
XML 2:
<?xml version="1.0" standalone="yes"?>
<Account>
<Table1>
<AccountNumber>5050</AccountNumber>
<ProductDate>2017-12-18</ProductDate>
<ProductNr>294</ProductNr>
</Table1>
</Account>
Så the result will be like this
XML 3:
<?xml version="1.0" standalone="yes"?>
<Account>
<Table1>
<Id>12</Id>
<AccountNumber>5050</AccountNumber>
<External>false</External>
<ProductDate>2017-12-18</ProductDate>
<ProductNr>294</ProductNr>
</Table1>
</Account>
You can use this:
public class XmlMatcher
{
public XDocument MatchXmlFiles(XDocument doc1, XDocument doc2)
{
XDocument result = new XDocument();
List<XElement> doc1Elements = doc1.Root.Elements().ToList();
List<XElement> doc2Elements = doc2.Root.Elements().ToList();
XElement accountElement = new XElement("Account");
result = new XDocument(accountElement);
for (int i = 0; i < doc1Elements.Count(); i++)
{
XmlDocument subDoc = new XmlDocument();
subDoc.LoadXml(doc1Elements[i].ToString());
string tableName = subDoc.FirstChild.Name;
string accountNumber = doc1Elements[i].Elements().Where(x => x.Name == "AccountNumber").FirstOrDefault().Value;
List<XElement> doc1Childs = doc1Elements[i].Elements().ToList();
List<XElement> doc2Childs = doc2Elements.Where(x => x.ToString().IndexOf(accountNumber) > -1).FirstOrDefault().Elements().ToList();
XElement tblElement = new XElement(tableName);
tblElement.Add(new XElement("Id" , GetChildValue(doc1Childs, doc2Childs, "Id")));
tblElement.Add(new XElement("AccountNumber", accountNumber));
tblElement.Add(new XElement("External" , GetChildValue(doc1Childs, doc2Childs, "External")));
tblElement.Add(new XElement("ProductDate" , GetChildValue(doc1Childs, doc2Childs, "ProductDate")));
tblElement.Add(new XElement("ProductNr" , GetChildValue(doc1Childs, doc2Childs, "ProductNr")));
accountElement.Add(tblElement);
}
return result;
}
public object GetChildValue(List<XElement> doc1Childs, List<XElement> doc2Childs, string name)
{
object value = string.Empty;
XElement child = doc1Childs.Where(x => x.Name == name).FirstOrDefault();
if (child != null)
{
value = child.Value;
}
else
{
child = doc2Childs.Where(x => x.Name == name).FirstOrDefault();
if(child != null)
value = child.Value;
}
return value;
}
}
Usage:
XmlMatcher xmlManager = new XmlMatcher();
XDocument xml1 = XDocument.Load("Your Xm1 Path");
XDocument xml2 = XDocument.Load("Your Xm2 Path");
XDocument xml3 = xmlManager.MatchXmlFiles(xml1, xml2);
xml3.Save("Your Xm3 Path");

How to format XDocument code to make it the most readable

I've written a method to serialize a list (containing class objects) into an xml string, but with so many nests I can't figure out how to format my code to make it the most readable. Here's the xml example:
<Text>
<Info name="example" language="en-US">example</Info>
<Info name="example" language="en-GB">example</Info>
</Text>
And here's the (most likely) terribly formatted code:
XDocument xdoc = new XDocument
(
new XElement
("Text",
Infos.Select(item =>
new XElement
("Info",
new XAttribute("name", item.Name),
new XAttribute("language", item.Language),
item.Value)
)
)
);
return xdoc.ToString();
This is a fairly short example, but it may grow in the future and as such, I'd like to make my code the most readable - how do I do that here?
I like this format
//Option 1
XDocument xdoc1 = new XDocument();
xdoc1.Add(new XElement("Text",
Infos.Select(item => new XElement("Info",
new XAttribute("name", item.Name),
new XAttribute("language", item.Language),
item.Value
)
)
));
//Option 2
XDocument xdoc2 = new XDocument();
xdoc2.Add(new XElement("Text", new object[] {
Infos.Select(item => new XElement("Info", new object[] {
new XAttribute("name", item.Name),
new XAttribute("language", item.Language),
item.Value
}))
}));

read xml document and update all fields c#

i'm trying to read xml file and update all it's value my xml was
<adf>
<prospect>
<requestdate>2015-10-29 07-38-22</requestdate>
<id sequence="1" source="admin.ss.com">admin.ss.com</id>
<vehicle interest="buy" status="">
<id sequence="1" source=""></id>
<year></year>
<make></make>
<model>camry</model>
<vin></vin>
<stock></stock>
<trim></trim>
</vehicle>
<customer>
<contact primarycontact="1">
<name part="first">Jessica</name>
<name part="last">Sonntag</name>
<email>js#test.com</email>
<phone type="phone" time="day">555-585-5555</phone>
<address>
<street line="1"></street>
<city></city>
<regioncode></regioncode>
<postalcode></postalcode>
<country></country>
</address>
</contact>
<comments>Vehicle Year: 2011 Comments: </comments>
</customer>
<provider>
<name part="full">ST</name>
<service> Engine Marketing</service>
<phone>1-866-572-3952</phone>
</provider>
</prospect>
</adf>
so i select node like below
var items = (from item in xmlDoc.Descendants("requestdate")
select item).ToList();
then i can update only requestdata tag value so do i have to repeat same for all tags or is there any good way to accomplish this.
Regards
There is an easy way to do this. This one is a hidden gem. Most people may not know this. This feature came in VS2013 and it's called "Paste XML as Classes."
Save your xml (Ex: MyXml.XML)
Create a new Console project
Open the Xml in Visual studio
Copy All contents of the xml (Ctl+A, Ctl + C)
Add a new class to your project. You can give any name you like.
Go to Edit>Paste Special>Paste XML as classes.
Add another class to your project. Then add below two methods to that class.
public static string Serialise<T>(T serialisableObject)
{
var doc = new XmlDocument();
using (var stream = new StringWriter())
{
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlWriter.Create(stream, settings);
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(xmlWriter, serialisableObject, ns);
doc.LoadXml(stream.ToString());
}
return doc.InnerXml;
}
public static T Deserialise<T>(string xml)
{
T list;
using (var reader = new StringReader(xml))
{
var serialiser = new XmlSerializer(typeof(T));
list = (T)serialiser.Deserialize(reader);
}
return list;
}
Then in your console applications Main method; add this.
var myObj = new adf();
myObj.prospect = new adfProspect();
myObj.prospect.customer = new adfProspectCustomer(){comments = "dgsrtetetete"};
//populate all fields.....
var xml = MySerializer.Serialise(myObj);
File.WriteAllText(#"C:\myNewXml.xml", xml);
That's it. Same way now you can deserialise an xml object in to your class.
Try the XmlSerializer class: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx If you serialize/deserialize the xml then up dating is trivial.
If you wanted to change every phone number to "0123456789" you could do something like:
var xDoc = XDocument.Load("document.xml");
var results = from phone in xDoc.Descendants("phone") select phone;
foreach (XElement result in results)
{
element.SetValue("0123456789");
}
i have came up with solution with support two extension method i'm iterating all nodes and update.(since my xml is not too big or complicated this one would be a good solution)
with help of these two extension methods
public static void IterateThroughAllNodes(this XmlDocument doc, Action<XmlNode> elementVisitor)
{
if (doc != null && elementVisitor != null)
{
foreach (XmlNode node in doc.ChildNodes)
{
DoIterateNode(node, elementVisitor);
}
}
}
public static void IterateThrough(this XmlNodeList nodes, Action<XmlNode> elementVisitor)
{
if (nodes != null && elementVisitor != null)
{
foreach (XmlNode node in nodes)
{
DoIterateNode(node, elementVisitor);
}
}
}
private static void DoIterateNode(XmlNode node, Action<XmlNode> elementVisitor)
{
elementVisitor(node);
foreach (XmlNode childNode in node.ChildNodes)
{
DoIterateNode(childNode, elementVisitor);
}
}
then i can update my xml nodes as below
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/xmlmail.xml"));
var email = new XmlEmail();
doc.IterateThroughAllNodes(
delegate(XmlNode node)
{
if (node.Name.Equals("requestdate"))
node.InnerText= email.RequestDate.ToLongDateString();
if (node.Name.Equals("vehicle"))
{
XmlNodeList childs = node.ChildNodes;
childs.IterateThrough(delegate(XmlNode vnode)
{
if (vnode.Name.Equals("id"))
vnode.InnerText= email.VehicleId.ToString();
if (vnode.Name.Equals("year"))
vnode.InnerText= email.Year.ToString();
if (vnode.Name.Equals("make"))
vnode.InnerText= email.Make;
if (vnode.Name.Equals("model"))
vnode.InnerText= email.Model;
if (vnode.Name.Equals("vin"))
vnode.InnerText= email.Vin;
if (vnode.Name.Equals("trim"))
vnode.InnerText = email.Trim;
});
}
if (node.Name.Equals("customer"))
{
XmlNodeList childs = node.ChildNodes;
childs.IterateThrough(delegate(XmlNode vnode)
{
if (vnode.Attributes != null && (vnode.Name.Equals("name") && vnode.Attributes["part"].Value.Equals("first")))
vnode.InnerText= email.FirstName;
if (vnode.Attributes != null && (vnode.Name.Equals("name") && vnode.Attributes["part"].Value.Equals("last")))
vnode.InnerText= email.LastName;
if (vnode.Name.Equals("email"))
vnode.InnerText= email.Email;
if (vnode.Name.Equals("phone"))
vnode.InnerText= email.Phone;
if (vnode.Name.Equals("comments"))
vnode.InnerText= email.Comments;
if (vnode.Name.Equals("address"))
{
XmlNodeList addresschilds = vnode.ChildNodes;
addresschilds.IterateThrough(delegate(XmlNode anode)
{
if (anode.Name.Equals("street"))
anode.InnerText= email.Street;
if (anode.Name.Equals("city"))
anode.InnerText= email.City;
if (anode.Name.Equals("phone"))
anode.InnerText= email.Phone;
if (anode.Name.Equals("regioncode"))
anode.InnerText= email.RegionCode;
if (anode.Name.Equals("postalcode"))
anode.InnerText= email.Postalode;
if (anode.Name.Equals("country"))
anode.InnerText= email.Country;
});
}
});
}
});

Select number of data based of a specific node

I have the following piece of code
XmlDocument docu = new XmlDocument();
docu.Load(file);
XmlNodeList lst = docu.GetElementsByTagName("name");
foreach (XmlNode n in lst)
{
string text = n.InnerText;
var types = doc.Element("program").Element("program-function").Element("function").Descendants("type").Where(x => x.Value == text).Select(c => c.Value).ToArray();
}
my xml is as follows
<program>
<program-function>
<function>
<name>add</name>
<return-type>double</return-type>
<params>
<type>double</type>
<type-value>a</type-value>
<type>double</type>
<type-value>b</type-value>
<type>string</type>
<type-value>c</type-value>
</params>
<body> return a + b + c; </body>
</function>
<function>
<name>test</name>
<return-type>int</return-type>
<params>
<type>double</type>
<type-value>a</type-value>
<type>double</type>
<type-value>b</type-value>
</params>
<body> return a + b; </body>
</function>
</program-function>
</program>
i need to be able to get the number of <type> for each <name>
result for add should be 3 = types.count() = 3
result for test should be 2 = types.count() = 2
any advice?
EDIT : If i want to retrieve each value inside types? ie. add should contain a,b,c and test should contain a,b. Would love to store it in an array for easier retrieval
How about using Linq to Xml
var xDoc = XDocument.Parse(xml);
var functions = xDoc.Descendants("function")
.Select(f => new
{
Name = f.Element("name").Value,
Types = f.Descendants("type").Select(t=>t.Value).ToList(),
//Types = f.Descendants("type").Count()
TypeValues = f.Descendants("type-value").Select(t=>t.Value).ToList()
})
.ToList();
Try this:
XDocument doc = XDocument.Load(your file);
var vals = doc.Element("program").Element("program-function").Elements("function");
var result = vals.Select(i =>
new { name = i.Element("name"),
count = i.Elements("type").Count() }

Filtering by higher nodes attributes linq

I am trying to serialize and insert a new created object into a specific child node of an XDocument. I've managed to accomplish this but my code seems smelly.
How can I test against a higher nodes attribute value without chaining through the Parent property like I have done below?
XDocument xdoc = XDocument.Load(path);
var elements = (
from doc in xdoc.Descendants("Unit")
where doc.Parent.Parent.Attribute("name").Value == _UnitTypeName &&
doc.Parent.Parent.Parent.Parent.Attribute("name").Value == _UnitCategoryN
doc.Parent.Parent.Parent.Parent.Parent.Parent.Attribute("name").Value ==
select doc
);
foreach (var element in elements)
{
Unit unit =
new Unit
{
Armour = _Armour,
Attacks = _Attacks,
BallisticSkill = _BallisticSkill,
Composition = _Composition,
DedicatedTransport = _DedicatedTransport,
Initiative = _Initiative,
Leadership = _Leadership,
Options = _Options,
SaveThrow = _SaveThrow,
SpecialRules = _SpecialRules,
Strength = _Strength,
Toughness = _Toughness,
UnitName = _UnitName,
Weapons = _Weapons,
WeaponSkill = _WeaponSkill,
Wounds = _Wounds,
Points = _Points
};
XmlSerializer serializer = new XmlSerializer(typeof(Unit));
using (var stream = new MemoryStream())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(stream, unit, ns);
stream.Position = 0;
//using (XmlReader reader = XmlReader.Create(stream))
//{
// XElement xe = XElement.Load(reader);
XElement xe = XElement.Load(#"C:\Test\tempfile.xml"); // For some reason loading via MemoryStream messes with xml formatting
element.AddBeforeSelf(xe);
//}
}
break;
}
xdoc.Save(path);
This is the structure of the XML Document:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArmy>
<Army name="Tyranid">
<unit-category>
<UnitCategory name="Troops">
<unit-type>
<UnitType name="Infantry">
<unit>
<Unit points="5" name="Hornmagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
<Amount>0</Amount>
</Unit>
<Unit points="5" name="Termagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
<Amount>0</Amount>
</Unit>
</unit>
</UnitType>
</unit-type>
</UnitCategory>
</unit-category>
</Army>
</ArrayOfArmy>
You can use compound from clauses, which is similar to using nested foreach loops:
var elements = (
from army in xdoc.Descendants("Army")
where army.Attribute("name").Value == _ArmyName
from unitCategory in army.Descendants("UnitCategory")
where unitCategory.Attribute("name").Value == _UnitCategoryName
from unitType in unitCategory.Descendants("UnitType")
where unitType.Attribute("name").Value == _UnitTypeName
from unit in unitType.Descendants("Unit")
select unit
);
Note: the method syntax equivalent is SelectMany().

Categories