how to edit xml file in C#
<Family_Inventory>
<FamilyCategory>Curtain Panels
<FamilyName>System Panel
<FamilySymbol>Glazed</FamilySymbol>
<FamilySymbol>Wall</FamilySymbo开发者_开发技巧l>
</FamilyName>
</FamilyCategory>
<FamilyCategory>Curtain Panels
<FamilyName>Rectangular Mullion
<FamilySymbol>2.5" x 5" rectangular</FamilySymbol>
</FamilyName>
</FamilyCategory>
...........
...........// many other family categories
...........
</Family_Inventory>
i want create only one FamilyCategory tag based on its value as above is 'Curtain Panels' so output will be
<Family_Inventory>
<FamilyCategory>Curtain Panels
<FamilyName>System Panel
<FamilySymbol>Glazed</FamilySymbol>
<FamilySymbol>Wall</FamilySymbol>
</FamilyName>
<FamilyName>Rectangular Mullion
<FamilySymbol>2.5" x 5" rectangular</FamilySymbol>
</FamilyName>
</FamilyCategory>
...........
...........// many other family categories
...........
</Family_Inventory>
Please tell me how Can i do this?.
Thanks in Advance Regards, Nitin
You should read the documentation on XmlDocument and XmlNode and it's associated classes. There is also plenty of examples of how to use them online.
Here is an introduction to XML with C#.
The first thing that comes to my mind is to use linq to get the whole document... then just create two classes
class FamilyCategory
{
string name;
List<FamilyName> familyNames;
}
class FamilyName
{
string name;
List<string> familySymbol;
}
after you parse the whole document just sort them by familyCategory.. then you just go through the list. You create a new instance of FamilyCategory and add all the FamilyNames to its list... For each new Cateogry you create a new class... after that you just need to add them all back which is described Here
I've not tested this, but I think it's close. It's a very manual approach, and there may be a slicker way to do it, but it's just manually grouping the categories into a new xdocument called groupedCategoryDocument.
var doc = XDocument.Parse(xmlString);
var groupedCategoryDocument = XDocument.Parse("<Family_Inventory />");
foreach(var category in doc.Root.Elements("FamilyCategory"))
{
//create the category for the new document
var groupedCategory = new XElement("FamilyCategory", category.Text);
//get all of the families for this category
foreach(var familyName in category.Elements("FamilyName"))
{
//add to new document
groupedCategory.Add(XElement.Parse(familyName.ToString());
}
// add category to new document
groupedCategoryDocument.Add(groupedCategory);
}
On third preview, I think the XML you are trying to generate is too complex and I think this will be the easiest both in terms of generation & consumption
<Family_Inventory>
<Family category="{category-name}" name="{family-name}" symbol="{symbol-1}"/>
<Family category="{category-name}" name="{family-name}" symbol="{symbol-2}"/>
...
<Family category="{category-name}" name="{family-name}" symbol="{symbol-n}"/>
</Family_Inventory>
One possibility is to use XSLT and grouping by keys. In this case it is complicated by the use of mixed content for "category" and "name"; it makes the key construction non-obvious:
normalize-space(./node()[1])
The first child node of FamilyCategory is assumed to be a text node that contains the category name,; it is further trimmed w.r.t. white space.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:key name="category" match="FamilyCategory" use="normalize-space(./node()[1])" />
<xsl:template match="Family_Inventory">
<xsl:copy>
<xsl:for-each
select="FamilyCategory[count(. | key('category', normalize-space(./node()[1]))[1]) = 1]">
<xsl:copy>
<xsl:value-of select="normalize-space(./node()[1])" />
<xsl:for-each select="key('category', normalize-space(./node()[1]))/FamilyName">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
A good explanation of how it works is found here: http://www.jenitennison.com/xslt/grouping/muenchian.xml
精彩评论