Answers for "xml reader read xml"

C#
0

Read XML

from bs4 import BeautifulSoup
 
soup = BeautifulSoup(open('W2Testfile.xml', encoding='utf-8'), 'lxml')
 
# Just copy from and doc and lower search or write lower case
sub_id = soup.find('SubmissionId'.lower())
# Tag and text
print(sub_id)
print(sub_id.text)
 
#---| Take out  part eg doc 2,then <find_all> of a tag that there are several of
doc_2 = soup.find('returndata', {'documentcnt': '2'})
dep_detail =  doc_2.find_all('DependentDetail'.lower())
print('-' * 30)
print(dep_detail[0].find('dependentrelationshipcd'))
print(dep_detail[0].find('dependentrelationshipcd').text)
Posted by: Guest on May-15-2021
0

xml reader attributes

// Get Xml node attribute keys and values
static IEnumerable<(string key, string value)> GetNodeAttributes(XmlReader reader)
{
    for (int i = 0; i < reader.AttributeCount; i++)
    {
        reader.MoveToAttribute(i);
        yield return (reader.Name, reader.Value);
    }
}

// This can be used with a Linq selector
//to retrieve specific value from the attributes
static string GetAttributeValue(this IEnumerable<(string key, string value)> attributes, string attribute) =>
    attributes.Where(x => x.key == attribute).ToList()[0].value;

// Combine these methods like so to get a attribute neatly.
string value = GetNodeAttributes(reader).GetAttributeValue("name");
Posted by: Guest on September-07-2021

C# Answers by Framework

Browse Popular Code Answers by Language