Change the node names in an XML file using C#

后端 未结 8 1831
南笙
南笙 2020-12-09 17:51

I have a huge bunch of XML files with the following structure:


  someContent
  someType
&         


        
相关标签:
8条回答
  • 2020-12-09 18:26

    Perhaps a better solution would be to iterate through each node, and write the information out to a new document. Obviously, this will depend on how you will be using the data in future, but I'd recommend the same reformatting as FlySwat suggested...

    <stuff id="1">
        <content/>
    </stuff>
    

    I'd also suggest that using the XDocument that was recently added would be the best way to go about creating the new document.

    0 讨论(0)
  • 2020-12-09 18:36

    I am not an expert in XML, and in my case I just needed to make all tag names in a HTML file to upper case, for further manipulation in XmlDocument with GetElementsByTagName. The reason I needed upper case was that for XmlDocument the tag names are case sensitive (since it is XML), and I could not guarantee that my HTML-file had consistent case in the tag names.

    So I solved it like this: I used XDocument as an intermediate step, where you can rename elements (i.e. the tag name), and then loaded that into a XmlDocument. Here is my VB.NET-code (the C#-coding will be very similar).

    Dim x As XDocument = XDocument.Load("myFile.html")
    For Each element In x.Descendants()
      element.Name = element.Name.LocalName.ToUpper()
    Next
    Dim x2 As XmlDocument = New XmlDocument()
    x2.LoadXml(x.ToString())
    

    For my purpose it worked fine, though I understand that in certain cases this might not be a solution if you are dealing with a pure XML-file.

    0 讨论(0)
提交回复
热议问题