2
하이 기존 된 XMLNodes에 속성을 추가하면 XML 구조입니다내가 기존 XML file.Here 여러 태그에 attribut를 추가하려고
public static List<Cities> cities = new List<Cities>();
XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"D:\Projects IDE\Visual Studio\Tutorial\e-commerce\classModeling\GenerateXml file\GenerateXml file\bin\Debug\Planet.xml");
XmlAttribute xKey = xDoc.CreateAttribute("Id");
XmlElement root = xDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//Country");
int count = 0;
foreach(XmlNode node in nodes) {
string name = node.Attributes["CountryName"].Value;
foreach (var cityObj in cities)
{
xKey.Value = cityObj.cityInitial;
if(name == cityObj.cityName)
{
count++;
node.Attributes.Append(xKey);
Console.WriteLine(count);
}
}
}
xDoc.Save(@"D:\Projects IDE\Visual Studio\Tutorial\e-commerce\classModeling\GenerateXml file\GenerateXml file\bin\Debug\Planets.xml");
문제점이 코드는 XML 파일의 마지막 요소에 ID를 추가하는 것입니다 : 국가 tags.Here 내 코드입니다. 이제 처음에는 그 중 하나 조건 만 맞았지만 카운터를 추가 했으므로 그 조건이 실제로 179 번인 것으로 나타났습니다. 그럴 경우 왜 내가 단 하나의 속성 만 추가 했는가?