zoukankan
html css js c++ java
xml读写
namespace
ConvenanceCompany.SQLServerDAL
{
public
class
Company:ICompany
{
/**/
///
<summary>
///
Method to get Company Information
///
</summary>
///
<returns></returns>
public
CompanyInfo GetCompany(
string
CompanyInfoFileName)
{
//
Set up a return value
CompanyInfo company
=
null
;
//
Get Information from xmlfile
Dictionary
<
string
,
string
>
companyinfo
=
ReadCompanyInfoFile(CompanyInfoFileName);
//
Fill in company object
if
(companyinfo
!=
null
)
{
company
=
new
CompanyInfo();
if
(companyinfo.ContainsKey(
"
Name
"
))
company.Name
=
companyinfo[
"
Name
"
];
else
company.Name
=
""
;
if
(companyinfo.ContainsKey(
"
InternetAdress
"
))
company.InternetAddress
=
companyinfo[
"
InternetAdress
"
];
else
company.InternetAddress
=
""
;
if
(companyinfo.ContainsKey(
"
BriefIntroduction
"
))
company.BriefIntroduction
=
companyinfo[
"
BriefIntroduction
"
];
else
company.BriefIntroduction
=
""
;
if
(companyinfo.ContainsKey(
"
TelFirst
"
)
!=
null
)
company.TelFirst
=
companyinfo[
"
TelFirst
"
];
else
company.TelFirst
=
""
;
if
(companyinfo.ContainsKey(
"
TelSecond
"
))
company.TelSecond
=
companyinfo[
"
TelSecond
"
];
else
company.TelSecond
=
""
;
if
(companyinfo.ContainsKey(
"
TelMobile
"
))
company.TelMobile
=
companyinfo[
"
TelMobile
"
];
else
company.TelMobile
=
""
;
if
(companyinfo.ContainsKey(
"
Postalcode
"
))
company.Postalcode
=
companyinfo[
"
Postalcode
"
];
else
company.Postalcode
=
""
;
if
(companyinfo.ContainsKey(
"
Email
"
))
company.Email
=
companyinfo[
"
Email
"
];
else
company.Email
=
""
;
if
(companyinfo.ContainsKey(
"
Address
"
))
company.Address
=
companyinfo[
"
Address
"
];
else
company.Address
=
""
;
if
(companyinfo.ContainsKey(
"
Copyright
"
))
company.Copyright
=
companyinfo[
"
Copyright
"
];
else
company.Copyright
=
""
;
}
return
company;
}
public
Dictionary
<
string
,
string
>
ReadCompanyInfoFile(
string
CompanyInfoFileName)
{
//
Memory CompanyInfo file inforomation
Dictionary
<
string
,
string
>
Companyinfo
=
new
Dictionary
<
string
,
string
>
();
XmlTextReader readerCompanyInfo
=
null
;
string
startElement
=
""
;
string
endElement
=
""
;
string
textNodeContent
=
""
;
bool
identifierTextNodeContent
=
false
;
//
标记内容是否为空
try
{
readerCompanyInfo
=
new
XmlTextReader(CompanyInfoFileName);
while
(readerCompanyInfo.Read())
{
switch
(readerCompanyInfo.NodeType)
{
case
XmlNodeType.Element:
startElement
=
readerCompanyInfo.Name;
identifierTextNodeContent
=
false
;
break
;
case
XmlNodeType.Text:
identifierTextNodeContent
=
true
;
textNodeContent
=
readerCompanyInfo.Value;
break
;
case
XmlNodeType.EndElement:
endElement
=
readerCompanyInfo.Name;
if
(startElement
==
endElement)
{
if
(identifierTextNodeContent)
Companyinfo.Add(endElement, textNodeContent);
else
Companyinfo.Add(endElement,
""
);
}
break
;
}
}
}
catch
(XmlException )
{
Companyinfo
=
null
;
}
finally
{
//
Close CompanyInfo file!
if
(readerCompanyInfo
!=
null
)
readerCompanyInfo.Close();
}
return
Companyinfo;
}
/**/
///
<summary>
///
Mothod to modify companyinfo
///
</summary>
///
<returns></returns>
public
bool
ModifyCompany(
string
CompanyInfoFileName,
string
name,
string
internetAdress,
string
briefIntroduction,
string
telFirst,
string
telSecond,
string
telMobile,
string
postalcode,
string
email,
string
address,
string
copyright)
{
XmlTextWriter xmlCompanyInfo;
try
{
xmlCompanyInfo
=
new
XmlTextWriter(CompanyInfoFileName, System.Text.Encoding.GetEncoding(
"
GB2312
"
));
xmlCompanyInfo.Formatting
=
Formatting.Indented;
xmlCompanyInfo.Indentation
=
3
;
xmlCompanyInfo.WriteStartDocument();
xmlCompanyInfo.WriteStartElement(
"
Company
"
);
xmlCompanyInfo.WriteStartElement(
"
Name
"
);
xmlCompanyInfo.WriteString(name);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
InternetAdress
"
);
xmlCompanyInfo.WriteString(internetAdress);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
BriefIntroduction
"
);
xmlCompanyInfo.WriteString(briefIntroduction);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
Tel
"
);
xmlCompanyInfo.WriteStartElement(
"
TelFirst
"
);
xmlCompanyInfo.WriteString(telFirst);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
TelSecond
"
);
xmlCompanyInfo.WriteString(telSecond);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
TelMobile
"
);
xmlCompanyInfo.WriteString(telMobile);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
Postalcode
"
);
xmlCompanyInfo.WriteString(postalcode);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
Email
"
);
xmlCompanyInfo.WriteString(email);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
Address
"
);
xmlCompanyInfo.WriteString(address);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteStartElement(
"
Copyright
"
);
xmlCompanyInfo.WriteString(copyright);
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.WriteEndElement();
xmlCompanyInfo.Close();
}
catch
(XmlException e)
{
return
false
;
}
finally
{
//
if (xmlCompanyInfo != null)
/**/
///
/xmlCompanyInfo.Close();
}
return
true
;
}
}
}
上面是对下面xml文件进行读写:
<?
xml version="1.0" encoding="gb2312"
?>
<
Company
>
<
Name
/>
<
InternetAdress
/>
<
BriefIntroduction
/>
<
Tel
>
<
TelFirst
/>
<
TelSecond
/>
<
TelMobile
/>
</
Tel
>
<
Postalcode
/>
<
Email
/>
<
Address
/>
<
Copyright
/>
</
Company
>
查看全文
相关阅读:
C语言I博客作业05
C语言I博客作业04
C语言II博客作业04
C语言II博客作业03
C语言II—作业02
C语言II博客作业01
学期总结
C语言I博客作业08
C语言I博客作业07
C语言I博客作业06
原文地址:https://www.cnblogs.com/wuming/p/1222807.html
最新文章
Telnet配置
静态注入
C语言II—作业04
C语言II博客作业03
C语言II博客作业02
C语言II博客作业01
学期总结
C语言I博客作业08
C语言I博客作业07
C语言I博客作业06
热门文章
C语言I博客作业05
C语言I博客作业04
C语言II博客作业04
C语言II博客作业03
C语言II博客作业02
C语言II博客作业01
学期总结
C语言I博客作业08
C语言I博客作业07
C语言I博客作业06
Copyright © 2011-2022 走看看